Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mcmire opened this issue Dec 22, 2023 · 3 comments
Assignees
Labels
bug Something isn't working team-wallet-framework

Comments

@mcmire
Copy link
Contributor

mcmire commented Dec 22, 2023

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

@mcmire mcmire added the bug Something isn't working label Dec 22, 2023
@mcmire
Copy link
Contributor Author

mcmire commented Feb 9, 2024

The original author of Superstruct seems to have abandoned this project. It is in the hands of a different maintainer, but they are slow to merge changes and fix issues. I think we should fork Superstruct to our org for the time being so that we can get past this issue without having to convert this library to ESM.

@mcmire
Copy link
Contributor Author

mcmire commented Feb 9, 2024

I've created a PR on a fork of Superstruct to address this issue.

In the future, however, we might consider using a different library that is better maintained such as Zod.

@mcmire mcmire self-assigned this Feb 9, 2024
@mcmire
Copy link
Contributor Author

mcmire commented Mar 14, 2024

The problems with Superstruct seem to have been solved in the fork that we created, which we published as @metamask/superstruct 2.0.0: https://github.com/MetaMask/superstruct/releases/tag/v2.0.0. I tested this by checking out MetaMask/core#3645 and linking @metamask/utils to a local version which replaced superstruct with @metamask/superstruct, and I didn't see any @metamask/utils-related errors. So I believe we can close this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-wallet-framework
Projects
None yet
Development

No branches or pull requests

1 participant