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

Generated code by preset-env causes error on Node.js v12 and ES Modules #10548

Closed
shimataro opened this issue Oct 14, 2019 · 3 comments · Fixed by #10549 · May be fixed by #10853
Closed

Generated code by preset-env causes error on Node.js v12 and ES Modules #10548

shimataro opened this issue Oct 14, 2019 · 3 comments · Fixed by #10549 · May be fixed by #10853
Labels

Comments

@shimataro
Copy link
Contributor

@shimataro shimataro commented Oct 14, 2019

Bug Report

Current Behavior

$ node --experimental-modules dist/example.mjs
(node:5693) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:79
  let url = moduleWrapResolve(specifier, parentURL);
            ^

Error: Cannot find module /path/to/example/node_modules/core-js/modules/es.array.iterator imported from /path/to/example/dist/example.mjs
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:79:13)
    at Loader.resolve (internal/modules/esm/loader.js:73:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:152:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
    at link (internal/modules/esm/module_job.js:42:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Input Code

src/example.mjs (source code)

for (const x of [1, 2, 3]) {
  console.log(x);
}

dist/example.mjs (generated code)

import "core-js/modules/es.array.iterator";

for (const x of [1, 2, 3]) {
  console.log(x);
}

Expected behavior/code

node --experimental-modules dist/example.mjs
(node:5717) ExperimentalWarning: The ESM module loader is experimental.
1
2
3

Babel Configuration (.babelrc, package.json, cli command)

[
  "@babel/preset-env",
  {
    "targets": {
      "node": "8.5.0"
    },
    "useBuiltIns": "usage",
    "corejs": 3,
    "modules": false
  }
]

Environment

  • Babel version(s): 7.6.4
  • Node/npm version: Node v12.11.1/npm 6.11.3
  • OS: Ubuntu 18.04
  • Monorepo: no
  • How you are using Babel: cli

Possible Solution
Could you add .js extension when importing polyfill?

import "core-js/modules/es.array.iterator.js"; // needs ".js"

for (const x of [1, 2, 3]) {
  console.log(x);
}

Now, it will work properly!

$ node --experimental-modules dist/example.mjs
(node:5705) ExperimentalWarning: The ESM module loader is experimental.
1
2
3

Additional context/Screenshots
As of Node.js v12, extension requires on import syntax.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Oct 14, 2019

Hey @shimataro! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

shimataro added a commit to shimataro/babel that referenced this issue Oct 14, 2019
@JLHwung

This comment has been minimized.

Copy link
Contributor

@JLHwung JLHwung commented Oct 14, 2019

Although ESM requires explicit file extensions, you can always specify --es-module-specifier-resolution=node to restore CommonJS extension resolution:

node --experimental-modules --es-module-specifier-resolution=node dist/example.mjs
@shimataro

This comment has been minimized.

Copy link
Contributor Author

@shimataro shimataro commented Oct 16, 2019

@JLHwung
Thank you for your comment.

Yes, I'm using this option for now. But I think it's a makeshift, not a smart solution.

Since v12 and ESM will be standard in the near future, I think it's better to work by default, not to specify --es-module-specifier-resolution=node.

shimataro added a commit to shimataro/babel that referenced this issue Oct 19, 2019
…ansform-runtime babel#10548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.