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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: @babel/register does not load ESM modules from node_modules in node v22 #16462

Open
1 task
avaly opened this issue May 2, 2024 · 1 comment
Open
1 task

Comments

@avaly
Copy link

avaly commented May 2, 2024

馃捇

  • Would you like to work on a fix?

How are you using Babel?

@babel/register or @babel/node

Input code

use-npm-package.js:

console.log(require('p-map').default);

use-local-module.js:

console.log(require('./esm.mjs').run());

esm.mjs:

export function run() {
  return 'Hello world!';
}

Configuration file name

No response

Configuration

No Babel config provided.

Current and expected behavior

Loading ESM modules from current directory

Current & expected behavior:

$ node --experimental-require-module use-local-module.js 
Hello world!

$ node --require @babel/register --experimental-require-module use-local-module.js 
Hello world!

Loading ESM modules from node_modules

Current behavior without @babel/register & Expected behavior with @babel/register:

$ node --experimental-require-module use-npm-package.js 
[AsyncFunction: pMap]

Current behavior with @babel/register:

$ node --require @babel/register --experimental-require-module use-npm-package.js 
(node:179449) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/dev/temp/babel-bugs/node_modules/p-map/index.js:1
export default async function pMap(
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (node:internal/modules/cjs/loader:1389:18)
    at Module._compile (node:internal/modules/cjs/loader:1425:20)
    at Module._compile (/home/dev/temp/babel-bugs/node_modules/pirates/lib/index.js:117:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1523:16)
    at Object.newLoader [as .js] (/home/dev/temp/babel-bugs/node_modules/pirates/lib/index.js:121:7)
    at Module.load (node:internal/modules/cjs/loader:1287:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1103:12)
    at Module.require (node:internal/modules/cjs/loader:1310:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/home/dev/temp/babel-bugs/use-npm-package.js:1:13)
    at Module._compile (node:internal/modules/cjs/loader:1480:14)
    at Module._compile (/home/dev/temp/babel-bugs/node_modules/pirates/lib/index.js:117:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1564:10)
    at Object.newLoader [as .js] (/home/dev/temp/babel-bugs/node_modules/pirates/lib/index.js:121:7)
    at Module.load (node:internal/modules/cjs/loader:1287:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1103:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:168:12)
    at node:internal/main/run_main_module:30:49

Node.js v22.1.0

Environment

System:
OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
Binaries:
Node: 22.1.0
npm: 10.7.0
npmPackages:
@babel/register: 7.23.7
p-map: 7.0.2

Possible solution

No response

Additional context

Support for require()ing ESM modules added in node.js v22: nodejs/node#51977

@babel-bot
Copy link
Collaborator

Hey @avaly! 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants