@@ -69,9 +69,14 @@ const {
69
69
ERR_REQUIRE_ESM
70
70
} = require ( 'internal/errors' ) . codes ;
71
71
const { validateString } = require ( 'internal/validators' ) ;
72
+ const {
73
+ resolveMainPath,
74
+ shouldUseESMLoader,
75
+ runMainESM
76
+ } = require ( 'internal/bootstrap/pre_execution' ) ;
72
77
const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
73
78
74
- module . exports = { wrapSafe, Module } ;
79
+ module . exports = { wrapSafe, Module, toRealPath , readPackageScope } ;
75
80
76
81
let asyncESM , ModuleJob , ModuleWrap , kInstantiated ;
77
82
@@ -810,6 +815,10 @@ Module.prototype.load = function(filename) {
810
815
this . paths = Module . _nodeModulePaths ( path . dirname ( filename ) ) ;
811
816
812
817
const extension = findLongestRegisteredExtension ( filename ) ;
818
+ // allow .mjs to be overridden
819
+ if ( filename . endsWith ( '.mjs' ) && ! Module . _extensions [ '.mjs' ] ) {
820
+ throw new ERR_REQUIRE_ESM ( filename ) ;
821
+ }
813
822
Module . _extensions [ extension ] ( this , filename ) ;
814
823
this . loaded = true ;
815
824
@@ -823,14 +832,19 @@ Module.prototype.load = function(filename) {
823
832
if ( module !== undefined && module . module !== undefined ) {
824
833
if ( module . module . getStatus ( ) >= kInstantiated )
825
834
module . module . setExport ( 'default' , exports ) ;
826
- } else { // preemptively cache
835
+ } else {
836
+ // Preemptively cache
837
+ // We use a function to defer promise creation for async hooks.
827
838
ESMLoader . moduleMap . set (
828
839
url ,
829
- new ModuleJob ( ESMLoader , url , ( ) =>
840
+ // Module job creation will start promises.
841
+ // We make it a function to lazily trigger those promises
842
+ // for async hooks compatibility.
843
+ ( ) => new ModuleJob ( ESMLoader , url , ( ) =>
830
844
new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
831
845
this . setExport ( 'default' , exports ) ;
832
846
} )
833
- )
847
+ , false /* isMain */ , false /* inspectBrk */ )
834
848
) ;
835
849
}
836
850
}
@@ -859,15 +873,15 @@ Module.prototype.require = function(id) {
859
873
var resolvedArgv ;
860
874
let hasPausedEntry = false ;
861
875
862
- function wrapSafe ( filename , content ) {
876
+ function wrapSafe ( filename , content , cjsModuleInstance ) {
863
877
if ( patched ) {
864
878
const wrapper = Module . wrap ( content ) ;
865
879
return vm . runInThisContext ( wrapper , {
866
880
filename,
867
881
lineOffset : 0 ,
868
882
displayErrors : true ,
869
883
importModuleDynamically : experimentalModules ? async ( specifier ) => {
870
- const loader = await asyncESM . loaderPromise ;
884
+ const loader = asyncESM . ESMLoader ;
871
885
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
872
886
} : undefined ,
873
887
} ) ;
@@ -892,17 +906,16 @@ function wrapSafe(filename, content) {
892
906
]
893
907
) ;
894
908
} catch ( err ) {
895
- if ( experimentalModules ) {
909
+ if ( experimentalModules && process . mainModule === cjsModuleInstance )
896
910
enrichCJSError ( err ) ;
897
- }
898
911
throw err ;
899
912
}
900
913
901
914
if ( experimentalModules ) {
902
915
const { callbackMap } = internalBinding ( 'module_wrap' ) ;
903
916
callbackMap . set ( compiled . cacheKey , {
904
917
importModuleDynamically : async ( specifier ) => {
905
- const loader = await asyncESM . loaderPromise ;
918
+ const loader = asyncESM . ESMLoader ;
906
919
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
907
920
}
908
921
} ) ;
@@ -925,7 +938,7 @@ Module.prototype._compile = function(content, filename) {
925
938
}
926
939
927
940
maybeCacheSourceMap ( filename , content , this ) ;
928
- const compiledWrapper = wrapSafe ( filename , content ) ;
941
+ const compiledWrapper = wrapSafe ( filename , content , this ) ;
929
942
930
943
var inspectorWrapper = null ;
931
944
if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
@@ -981,7 +994,11 @@ Module._extensions['.js'] = function(module, filename) {
981
994
'files in that package scope as ES modules.\nInstead rename ' +
982
995
`${ basename } to end in .cjs, change the requiring code to use ` +
983
996
'import(), or remove "type": "module" from ' +
984
- `${ path . resolve ( pkg . path , 'package.json' ) } .`
997
+ `${ path . resolve ( pkg . path , 'package.json' ) } .` ,
998
+ undefined ,
999
+ undefined ,
1000
+ undefined ,
1001
+ true
985
1002
) ;
986
1003
warnRequireESM = false ;
987
1004
}
@@ -1024,26 +1041,15 @@ Module._extensions['.node'] = function(module, filename) {
1024
1041
return process . dlopen ( module , path . toNamespacedPath ( filename ) ) ;
1025
1042
} ;
1026
1043
1027
- Module . _extensions [ '.mjs' ] = function ( module , filename ) {
1028
- throw new ERR_REQUIRE_ESM ( filename ) ;
1029
- } ;
1030
-
1031
1044
// Bootstrap main module.
1032
- Module . runMain = function ( ) {
1033
- // Load the main module--the command line argument.
1034
- if ( experimentalModules ) {
1035
- asyncESM . loaderPromise . then ( ( loader ) => {
1036
- return loader . import ( pathToFileURL ( process . argv [ 1 ] ) . href ) ;
1037
- } )
1038
- . catch ( ( e ) => {
1039
- internalBinding ( 'errors' ) . triggerUncaughtException (
1040
- e ,
1041
- true /* fromPromise */
1042
- ) ;
1043
- } ) ;
1044
- return ;
1045
+ Module . runMain = function ( main = process . argv [ 1 ] ) {
1046
+ const resolvedMain = resolveMainPath ( main ) ;
1047
+ const useESMLoader = shouldUseESMLoader ( resolvedMain ) ;
1048
+ if ( useESMLoader ) {
1049
+ runMainESM ( resolvedMain || main ) ;
1050
+ } else {
1051
+ Module . _load ( main , null , true ) ;
1045
1052
}
1046
- Module . _load ( process . argv [ 1 ] , null , true ) ;
1047
1053
} ;
1048
1054
1049
1055
function createRequireFromPath ( filename ) {
@@ -1164,7 +1170,7 @@ Module.Module = Module;
1164
1170
1165
1171
// We have to load the esm things after module.exports!
1166
1172
if ( experimentalModules ) {
1167
- asyncESM = require ( 'internal/process/esm_loader' ) ;
1168
1173
ModuleJob = require ( 'internal/modules/esm/module_job' ) ;
1174
+ asyncESM = require ( 'internal/process/esm_loader' ) ;
1169
1175
( { ModuleWrap, kInstantiated } = internalBinding ( 'module_wrap' ) ) ;
1170
1176
}
0 commit comments