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

node.js 12.2.0 breaks patching of .mjs files #56

Open
coreyfarrell opened this issue Sep 26, 2019 · 4 comments
Open

node.js 12.2.0 breaks patching of .mjs files #56

coreyfarrell opened this issue Sep 26, 2019 · 4 comments

Comments

@coreyfarrell
Copy link

node -r @babel/register ./script.mjs worked in node.js 12.1.0 or below (as long as --experimental-modules was not passed to node). Since 12.2.0 this no longer works due to nodejs/node#27417. The problem is pirates now wraps a function which throws ERR_REQUIRE_ESM. It looks like the esm module calls require.extensions['.mjs']() if it exists and checks for the node.js core exception to know if it should wrap or replace the existing function. Maybe pirates could do something similar?

From @nicolo-ribaudo on https://babeljs.slack.com/:

We should overwrite that overwrite that hook as such:

  • If the file shouldn't be compiled by Babel, call the original hook (which will throw)
  • If the file is compiled by Babel and it's source type is still module, call the original hook
  • Otherwise, handle it like if it was a .js file

My idea is if the hooks do not transpile from ESM to CJS the original require.extensions['.js'] would throw a SyntaxError exception at which point the original require.extensions['.mjs'] could be called to throw ERR_REQUIRE_ESM?

CC @bcoe @jdalton

@bcoe
Copy link

bcoe commented Sep 27, 2019

We should probably loop @MylesBorins into this conversation as well, curious what his thoughts are.

@coreyfarrell
Copy link
Author

See also nodejs/modules#389 for node.js 12.11.0. Looks like they plan to put that change behind the --experimental-modules flag but this is a delay rather than a reversal.

@vjpr
Copy link

vjpr commented Sep 26, 2021

The solution here is to use --experimental-loader instead.

@AllTradesz
Copy link

The solution here is to use --experimental-loader instead.

Could you elaborate, provide an example? Would illuminate your meaning/proposal.

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

No branches or pull requests

4 participants