Invalid Import Path for core-js Imports When Using type: module
See original GitHub issueBug Report
- I would like to work on a fix!
Current behavior A clear and concise description of the behavior.
- REPL, Codesandbox, or GitHub Repo helps!
When I use @babel/preset-env
with the useBuiltIns
option, the core-js
imports still output without the .js
. This is usually fine, except when running in a node environment that supports built-in modules, e.g. with type: module
in the package.json or outputting to a file with the .mjs
extension.
This happens even if I use --out-file-extension .mjs
.
--out-file-extension .cjs
works, if you don’t use modules: false
, but then you’re not outputting modules.
Input Code
test.js
const greeting = new Set(['Hello', 'world!']);
console.log(Array.from(greeting).join(' '));
npx babel test.js -d test/
node test/test.js
Output:
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '<repo>/node_modules/core-js/modules/esnext.set.add-all' imported from <repo>/test/test.js
Did you mean to import core-js/modules/esnext.set.add-all.js?
at finalizeResolution (internal/modules/esm/resolve.js:275:11)
at packageResolve (internal/modules/esm/resolve.js:668:12)
at moduleResolve (internal/modules/esm/resolve.js:716:14)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:809:11)
at Loader.resolve (internal/modules/esm/loader.js:85:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
at link (internal/modules/esm/module_job.js:50:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Expected behavior A clear and concise description of what you expected to happen (or code).
No errors
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
- Filename:
babel.config.js
const presets = [['@babel/preset-env', {
targets: {node: true},
modules: false,
useBuiltIns: 'usage',
corejs: {version: 3, proposals: true},
}]];
export default {presets};
Environment
System:
OS: android
Binaries:
Node: 14.8.0 - /data/data/com.termux/files/usr/bin/node
npm: 6.14.7 - /data/data/com.termux/files/usr/bin/npm
npmPackages:
@babel/cli: ^7.10.5 => 7.10.5
@babel/core: ^7.11.4 => 7.11.4
@babel/preset-env: ^7.11.0 => 7.11.0
Possible Solution
One possible solution would be to always output with the extension. This would create longer output, but it would be future-proof and wouldn’t require the user to do anything.
The best solution would probably be to guess whether the extension is needed, based on whether the package.json has type: module
, and have a configuration option like addExtension
or addCorejsExtension
to force it one way or the other.
The last option I can think of is to always output .cjs
, or add a configuration like changeExtension
. .mjs
would output to a .mjs
file AND add the corejs
path extensions. .cjs
would output to .cjs
and wouldn’t add the extensions. .js
, the default, would output to .js
and possibly guess or use the addCorejsExtension
option. I notice there is a --out-file-extension
option for @babel/cli
: having this keep the corejs
extensions when you use .mjs
would be fine, too.
Additional context Add any other context about the problem here. Or a screenshot if applicable
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
As a workaround, it should already work when using the new
babel-plugin-polyfill-corejs3
from https://github.com/babel/babel-polyfillsAh okay, OP seemed like it was only about import statements injected by
@babel/plugin-transform-runtime
, not the files within@babel/runtime
.