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
node -r @babel/register ./script.mjsworked in node.js 12.1.0 or below (as long as--experimental-moduleswas not passed to node). Since 12.2.0 this no longer works due to nodejs/node#27417. The problem ispiratesnow wraps a function which throwsERR_REQUIRE_ESM. It looks like theesmmodule callsrequire.extensions['.mjs']()if it exists and checks for the node.js core exception to know if it should wrap or replace the existing function. Maybepiratescould do something similar?From @nicolo-ribaudo on https://babeljs.slack.com/:
My idea is if the hooks do not transpile from ESM to CJS the original
require.extensions['.js']would throw aSyntaxErrorexception at which point the originalrequire.extensions['.mjs']could be called to throw ERR_REQUIRE_ESM?CC @bcoe @jdalton