Skip to content

Cannot use this in a project with module and moduleResolution of nodenext/node16 #163

Closed
@mcmire

Description

When using this library in a project with module and moduleResolution of nodenext or node16, TypeScript produces compilation errors stemming from Superstruct:

node_modules/superstruct/dist/index.d.ts:1:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

1 export * from './error';
                ~~~~~~~~~

node_modules/superstruct/dist/index.d.ts:2:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

2 export * from './struct';
                ~~~~~~~~~~

node_modules/superstruct/dist/index.d.ts:3:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

3 export * from './structs/coercions';
                ~~~~~~~~~~~~~~~~~~~~~

...

It seems that Superstruct is a ESM library (because its type field in package.json is "module"), but its published TypeScript definition files aren't ESM-compatible. This is tracked a bug here: ianstormtaylor/superstruct#1160

Unfortunately you cannot just change the file extensions, because this library (utils) isn't an ESM library, and you can't import an ESM library in a CommonJS library or else you get an error from TypeScript:

src/versions.ts:9:36 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("superstruct")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/Users/elliot/code/metamask/utils/package.json'.

9 import { is, refine, string } from 'superstruct';

So in order to use Superstruct, we either need to fork and fix Superstruct so that it's CommonJS-compatible, or we need to convert this library to use ESM (and make sure it's CommonJS-compatible).

I've started a branch which does both of these things here: convert-to-esm

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions