question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid Import Path for core-js Imports When Using type: module

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current behavior A clear and concise description of the behavior.

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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Aug 25, 2020

As a workaround, it should already work when using the new babel-plugin-polyfill-corejs3 from https://github.com/babel/babel-polyfills

0reactions
jedwards1211commented, Oct 9, 2020

Ah okay, OP seemed like it was only about import statements injected by @babel/plugin-transform-runtime, not the files within @babel/runtime.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'core-js/es6'
I cross referenced with Angular source expecting to see this newer single import - and even the 'hello world Ivy' example still shows...
Read more >
Using TypeScript or ES6 – amCharts 4 Documentation
When importing the module, use its npm name @amcharts/amcharts4/... . IMPORTANT. It is currently impossible to load npm packages directly in the browser....
Read more >
Shimming - webpack
The webpack compiler can understand modules written as ES2015 modules, CommonJS or AMD. However, some third party libraries may expect global dependencies ...
Read more >
Common Error Details - Snowpack
If you are using TypeScript, this error could occur if you are importing or exporting something that only exists in TypeScript (like a...
Read more >
Node.js v19.3.0 Documentation
_unrefActive(); DEP0128: modules with an invalid main entry and an index.js file ... JSON modules; Wasm modules; Top-level await; HTTPS and HTTP imports....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found