@@ -70,9 +70,14 @@ const {
70
70
ERR_REQUIRE_ESM
71
71
} = require ( 'internal/errors' ) . codes ;
72
72
const { validateString } = require ( 'internal/validators' ) ;
73
+ const {
74
+ resolveMainPath,
75
+ shouldUseESMLoader,
76
+ runMainESM
77
+ } = require ( 'internal/bootstrap/pre_execution' ) ;
73
78
const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
74
79
75
- module . exports = { wrapSafe, Module } ;
80
+ module . exports = { wrapSafe, Module, toRealPath , readPackageScope } ;
76
81
77
82
let asyncESM , ModuleJob , ModuleWrap , kInstantiated ;
78
83
@@ -898,6 +903,10 @@ Module.prototype.load = function(filename) {
898
903
this . paths = Module . _nodeModulePaths ( path . dirname ( filename ) ) ;
899
904
900
905
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
+ }
901
910
Module . _extensions [ extension ] ( this , filename ) ;
902
911
this . loaded = true ;
903
912
@@ -911,14 +920,19 @@ Module.prototype.load = function(filename) {
911
920
if ( module !== undefined && module . module !== undefined ) {
912
921
if ( module . module . getStatus ( ) >= kInstantiated )
913
922
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.
915
926
ESMLoader . moduleMap . set (
916
927
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 , ( ) =>
918
932
new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
919
933
this . setExport ( 'default' , exports ) ;
920
934
} )
921
- )
935
+ , false /* isMain */ , false /* inspectBrk */ )
922
936
) ;
923
937
}
924
938
}
@@ -947,15 +961,15 @@ Module.prototype.require = function(id) {
947
961
let resolvedArgv ;
948
962
let hasPausedEntry = false ;
949
963
950
- function wrapSafe ( filename , content ) {
964
+ function wrapSafe ( filename , content , cjsModuleInstance ) {
951
965
if ( patched ) {
952
966
const wrapper = Module . wrap ( content ) ;
953
967
return vm . runInThisContext ( wrapper , {
954
968
filename,
955
969
lineOffset : 0 ,
956
970
displayErrors : true ,
957
971
importModuleDynamically : experimentalModules ? async ( specifier ) => {
958
- const loader = await asyncESM . loaderPromise ;
972
+ const loader = asyncESM . ESMLoader ;
959
973
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
960
974
} : undefined ,
961
975
} ) ;
@@ -981,17 +995,16 @@ function wrapSafe(filename, content) {
981
995
]
982
996
) ;
983
997
} catch ( err ) {
984
- if ( experimentalModules ) {
998
+ if ( experimentalModules && process . mainModule === cjsModuleInstance )
985
999
enrichCJSError ( err ) ;
986
- }
987
1000
throw err ;
988
1001
}
989
1002
990
1003
if ( experimentalModules ) {
991
1004
const { callbackMap } = internalBinding ( 'module_wrap' ) ;
992
1005
callbackMap . set ( compiled . cacheKey , {
993
1006
importModuleDynamically : async ( specifier ) => {
994
- const loader = await asyncESM . loaderPromise ;
1007
+ const loader = asyncESM . ESMLoader ;
995
1008
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
996
1009
}
997
1010
} ) ;
@@ -1014,7 +1027,7 @@ Module.prototype._compile = function(content, filename) {
1014
1027
}
1015
1028
1016
1029
maybeCacheSourceMap ( filename , content , this ) ;
1017
- const compiledWrapper = wrapSafe ( filename , content ) ;
1030
+ const compiledWrapper = wrapSafe ( filename , content , this ) ;
1018
1031
1019
1032
var inspectorWrapper = null ;
1020
1033
if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
@@ -1070,7 +1083,11 @@ Module._extensions['.js'] = function(module, filename) {
1070
1083
'files in that package scope as ES modules.\nInstead rename ' +
1071
1084
`${ basename } to end in .cjs, change the requiring code to use ` +
1072
1085
'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
1074
1091
) ;
1075
1092
warnRequireESM = false ;
1076
1093
}
@@ -1113,26 +1130,16 @@ Module._extensions['.node'] = function(module, filename) {
1113
1130
return process . dlopen ( module , path . toNamespacedPath ( filename ) ) ;
1114
1131
} ;
1115
1132
1116
- Module . _extensions [ '.mjs' ] = function ( module , filename ) {
1117
- throw new ERR_REQUIRE_ESM ( filename ) ;
1118
- } ;
1119
-
1120
1133
// 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 ) ;
1134
1142
}
1135
- Module . _load ( process . argv [ 1 ] , null , true ) ;
1136
1143
} ;
1137
1144
1138
1145
function createRequireFromPath ( filename ) {
@@ -1238,7 +1245,7 @@ Module.Module = Module;
1238
1245
1239
1246
// We have to load the esm things after module.exports!
1240
1247
if ( experimentalModules ) {
1241
- asyncESM = require ( 'internal/process/esm_loader' ) ;
1242
1248
ModuleJob = require ( 'internal/modules/esm/module_job' ) ;
1249
+ asyncESM = require ( 'internal/process/esm_loader' ) ;
1243
1250
( { ModuleWrap, kInstantiated } = internalBinding ( 'module_wrap' ) ) ;
1244
1251
}
0 commit comments