@@ -70,9 +70,14 @@ const {
7070 ERR_REQUIRE_ESM
7171} = require ( 'internal/errors' ) . codes ;
7272const { validateString } = require ( 'internal/validators' ) ;
73+ const {
74+ resolveMainPath,
75+ shouldUseESMLoader,
76+ runMainESM
77+ } = require ( 'internal/bootstrap/pre_execution' ) ;
7378const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
7479
75- module . exports = { wrapSafe, Module } ;
80+ module . exports = { wrapSafe, Module, toRealPath , readPackageScope } ;
7681
7782let asyncESM , ModuleJob , ModuleWrap , kInstantiated ;
7883
@@ -898,6 +903,10 @@ Module.prototype.load = function(filename) {
898903 this . paths = Module . _nodeModulePaths ( path . dirname ( filename ) ) ;
899904
900905 const extension = findLongestRegisteredExtension ( filename ) ;
906+ // allow .mjs to be overridden
907+ if ( filename . endsWith ( '.mjs' ) && ! Module . _extensions [ '.mjs' ] ) {
908+ throw new ERR_REQUIRE_ESM ( filename ) ;
909+ }
901910 Module . _extensions [ extension ] ( this , filename ) ;
902911 this . loaded = true ;
903912
@@ -911,14 +920,19 @@ Module.prototype.load = function(filename) {
911920 if ( module !== undefined && module . module !== undefined ) {
912921 if ( module . module . getStatus ( ) >= kInstantiated )
913922 module . module . setExport ( 'default' , exports ) ;
914- } else { // preemptively cache
923+ } else {
924+ // Preemptively cache
925+ // We use a function to defer promise creation for async hooks.
915926 ESMLoader . moduleMap . set (
916927 url ,
917- new ModuleJob ( ESMLoader , url , ( ) =>
928+ // Module job creation will start promises.
929+ // We make it a function to lazily trigger those promises
930+ // for async hooks compatibility.
931+ ( ) => new ModuleJob ( ESMLoader , url , ( ) =>
918932 new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
919933 this . setExport ( 'default' , exports ) ;
920934 } )
921- )
935+ , false /* isMain */ , false /* inspectBrk */ )
922936 ) ;
923937 }
924938 }
@@ -947,15 +961,15 @@ Module.prototype.require = function(id) {
947961let resolvedArgv ;
948962let hasPausedEntry = false ;
949963
950- function wrapSafe ( filename , content ) {
964+ function wrapSafe ( filename , content , cjsModuleInstance ) {
951965 if ( patched ) {
952966 const wrapper = Module . wrap ( content ) ;
953967 return vm . runInThisContext ( wrapper , {
954968 filename,
955969 lineOffset : 0 ,
956970 displayErrors : true ,
957971 importModuleDynamically : experimentalModules ? async ( specifier ) => {
958- const loader = await asyncESM . loaderPromise ;
972+ const loader = asyncESM . ESMLoader ;
959973 return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
960974 } : undefined ,
961975 } ) ;
@@ -981,17 +995,16 @@ function wrapSafe(filename, content) {
981995 ]
982996 ) ;
983997 } catch ( err ) {
984- if ( experimentalModules ) {
998+ if ( experimentalModules && process . mainModule === cjsModuleInstance )
985999 enrichCJSError ( err ) ;
986- }
9871000 throw err ;
9881001 }
9891002
9901003 if ( experimentalModules ) {
9911004 const { callbackMap } = internalBinding ( 'module_wrap' ) ;
9921005 callbackMap . set ( compiled . cacheKey , {
9931006 importModuleDynamically : async ( specifier ) => {
994- const loader = await asyncESM . loaderPromise ;
1007+ const loader = asyncESM . ESMLoader ;
9951008 return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
9961009 }
9971010 } ) ;
@@ -1014,7 +1027,7 @@ Module.prototype._compile = function(content, filename) {
10141027 }
10151028
10161029 maybeCacheSourceMap ( filename , content , this ) ;
1017- const compiledWrapper = wrapSafe ( filename , content ) ;
1030+ const compiledWrapper = wrapSafe ( filename , content , this ) ;
10181031
10191032 var inspectorWrapper = null ;
10201033 if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
@@ -1070,7 +1083,11 @@ Module._extensions['.js'] = function(module, filename) {
10701083 'files in that package scope as ES modules.\nInstead rename ' +
10711084 `${ basename } to end in .cjs, change the requiring code to use ` +
10721085 'import(), or remove "type": "module" from ' +
1073- `${ path . resolve ( pkg . path , 'package.json' ) } .`
1086+ `${ path . resolve ( pkg . path , 'package.json' ) } .` ,
1087+ undefined ,
1088+ undefined ,
1089+ undefined ,
1090+ true
10741091 ) ;
10751092 warnRequireESM = false ;
10761093 }
@@ -1113,26 +1130,16 @@ Module._extensions['.node'] = function(module, filename) {
11131130 return process . dlopen ( module , path . toNamespacedPath ( filename ) ) ;
11141131} ;
11151132
1116- Module . _extensions [ '.mjs' ] = function ( module , filename ) {
1117- throw new ERR_REQUIRE_ESM ( filename ) ;
1118- } ;
1119-
11201133// Bootstrap main module.
1121- Module . runMain = function ( ) {
1122- // Load the main module--the command line argument.
1123- if ( experimentalModules ) {
1124- asyncESM . loaderPromise . then ( ( loader ) => {
1125- return loader . import ( pathToFileURL ( process . argv [ 1 ] ) . href ) ;
1126- } )
1127- . catch ( ( e ) => {
1128- internalBinding ( 'errors' ) . triggerUncaughtException (
1129- e ,
1130- true /* fromPromise */
1131- ) ;
1132- } ) ;
1133- return ;
1134+ Module . runMain = function ( main = process . argv [ 1 ] ) {
1135+ const resolvedMain = resolveMainPath ( main ) ;
1136+ const useESMLoader = shouldUseESMLoader ( resolvedMain ) ;
1137+ module . exports . asyncRunMain = useESMLoader ;
1138+ if ( useESMLoader ) {
1139+ runMainESM ( resolvedMain || main ) ;
1140+ } else {
1141+ Module . _load ( main , null , true ) ;
11341142 }
1135- Module . _load ( process . argv [ 1 ] , null , true ) ;
11361143} ;
11371144
11381145function createRequireFromPath ( filename ) {
@@ -1238,7 +1245,7 @@ Module.Module = Module;
12381245
12391246// We have to load the esm things after module.exports!
12401247if ( experimentalModules ) {
1241- asyncESM = require ( 'internal/process/esm_loader' ) ;
12421248 ModuleJob = require ( 'internal/modules/esm/module_job' ) ;
1249+ asyncESM = require ( 'internal/process/esm_loader' ) ;
12431250 ( { ModuleWrap, kInstantiated } = internalBinding ( 'module_wrap' ) ) ;
12441251}
0 commit comments