diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index c37233ffd45252..bc7ecf9f16c7f8 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -692,19 +692,13 @@ Module.prototype._compile = function(content, filename) { content = stripShebang(content); - // create wrapper function - var wrapper = Module.wrap(content); - - var compiledWrapper = vm.runInThisContext(wrapper, { - filename: filename, - lineOffset: 0, - displayErrors: true, - importModuleDynamically: experimentalModules ? async (specifier) => { - if (asyncESM === undefined) lazyLoadESM(); - const loader = await asyncESM.loaderPromise; - return loader.import(specifier, normalizeReferrerURL(filename)); - } : undefined, - }); + const compiledWrapper = vm.compileFunction(content, [ + 'exports', + 'require', + 'module', + '__filename', + '__dirname', + ], { filename }); var inspectorWrapper = null; if (process._breakFirstLine && process._eval == null) {