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

ESM builds are unusable in NodeJS #6127

Closed
vladfrangu opened this issue May 29, 2023 · 10 comments
Closed

ESM builds are unusable in NodeJS #6127

vladfrangu opened this issue May 29, 2023 · 10 comments
Assignees
Labels
4.x 4.0 related Bug Addressing a bug

Comments

@vladfrangu
Copy link

Expected behavior

Importing the module in an ESM-based project should not throw errors

Actual behavior

Importing the module in an ESM-based project throws errors related to missing file extensions.

❯ node test.mjs                                  
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/vlad/Development/test/node_modules/web3/lib/esm/web3' imported from /Users/vlad/Development/test/node_modules/web3/lib/esm/index.js
    at new NodeError (node:internal/errors:399:5)
    at finalizeResolution (node:internal/modules/esm/resolve:326:11)
    at moduleResolve (node:internal/modules/esm/resolve:945:10)
    at defaultResolve (node:internal/modules/esm/resolve:1153:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v18.16.0

Running node with --experimental-specifier-resolution=node throws a separate error related to lack of import assertions on JSON files.

❯ node --experimental-specifier-resolution=node test.mjs 
(node:50994) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///Users/vlad/Development/test/node_modules/web3-eth-accounts/lib/esm/common/chains/goerli.json" needs an import assertion of type "json"
    at new NodeError (node:internal/errors:399:5)
    at validateAssertions (node:internal/modules/esm/assert:95:15)
    at defaultLoad (node:internal/modules/esm/load:84:3)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at #createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
    at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:21) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}

Node.js v18.16.0

Steps to reproduce the behavior

  1. Install web3@4.0.1-rc.1
  2. Create an mjs file (test.mjs)
  3. Try the following code
import { Web3 } from 'web3';

const web3 = new Web3('http://localhost:8545');

Environment

  • nodejs 18.16.0
  • npm 9.5.1
  • yarn 3.5.1
  • web3 4.0.1-rc.1
@jdevcs
Copy link
Contributor

jdevcs commented May 30, 2023

@vladfrangu Thanks for testing RC, this issue is fixed under #6032 and another RC with fix will be released in this week.

@jdevcs jdevcs added 4.x 4.0 related Bug Addressing a bug labels May 30, 2023
@vladfrangu
Copy link
Author

Awesome! Just one curiosity, does that also fix the missing .js extensions in files imported in ESM builds?

@luu-alex
Copy link
Contributor

@vladfrangu what does the error output look like for missing .js extensions?

@vladfrangu
Copy link
Author

See the first error sent in the issue report!

@jdevcs
Copy link
Contributor

jdevcs commented May 30, 2023

For

missing .js extensions in files imported in ESM builds?

there will still need of --experimental-specifier-resolution=node

@vladfrangu
Copy link
Author

Ideally tho that shouldn't be needed! Not only because its experimental (so we don't know if it'll even remain in node, although it's likely it will remain), but because the fix is quite simple (which is just appending .js to every import statement, or using a bundler like tsup) 😅

@luu-alex
Copy link
Contributor

luu-alex commented May 30, 2023

Thanks @vladfrangu i'll be taking a look into getting rid of the experimental flag and seeing if I can get this out before release, if not it'll be apart of the next update.

@luu-alex luu-alex mentioned this issue May 31, 2023
17 tasks
@luu-alex
Copy link
Contributor

@vladfrangu thank you so much for creating this issue and even providing me an idea on how to do it :) created a PR and hopefully will be merged soon after feedback.

@jdevcs
Copy link
Contributor

jdevcs commented May 31, 2023

#5994

@jdevcs
Copy link
Contributor

jdevcs commented May 31, 2023

Thanks @luu-alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

4 participants