diff --git a/dist-raw/runmain-hack.js b/dist-raw/runmain-hack.js new file mode 100644 index 000000000..6c0688e14 --- /dev/null +++ b/dist-raw/runmain-hack.js @@ -0,0 +1,9 @@ +const {pathToFileURL} = require('url'); + +// Hack to avoid Module.runMain on node 18.6.0 +// Keeping it simple for now, isolated in this file. +// Could theoretically probe `getFormat` impl to determine if `import()` or `Module._load()` is best +// Note that I attempted a try-catch around `Module._load`, but it poisons some sort of cache such that subsequent `import()` is impossible. +exports.run = function(entryPointPath) { + import(pathToFileURL(entryPointPath)); +} diff --git a/src/bin.ts b/src/bin.ts index fb3208c48..f470a2c83 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -639,7 +639,15 @@ function phase4(payload: BootstrapState) { // Execute the main contents (either eval, script or piped). if (executeEntrypoint) { - Module.runMain(); + if ( + payload.isInChildProcess && + versionGteLt(process.versions.node, '18.6.0') + ) { + // HACK workaround node regression + require('../dist-raw/runmain-hack.js').run(entryPointPath); + } else { + Module.runMain(); + } } else { // Note: eval and repl may both run, but never with stdin. // If stdin runs, eval and repl will not.