@@ -69,10 +69,15 @@ 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
const experimentalExports = getOptionValue ( '--experimental-exports' ) ;
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
@@ -807,6 +812,10 @@ Module.prototype.load = function(filename) {
807
812
this . paths = Module . _nodeModulePaths ( path . dirname ( filename ) ) ;
808
813
809
814
const extension = findLongestRegisteredExtension ( filename ) ;
815
+ // allow .mjs to be overridden
816
+ if ( filename . endsWith ( '.mjs' ) && ! Module . _extensions [ '.mjs' ] ) {
817
+ throw new ERR_REQUIRE_ESM ( filename ) ;
818
+ }
810
819
Module . _extensions [ extension ] ( this , filename ) ;
811
820
this . loaded = true ;
812
821
@@ -820,14 +829,19 @@ Module.prototype.load = function(filename) {
820
829
if ( module !== undefined && module . module !== undefined ) {
821
830
if ( module . module . getStatus ( ) >= kInstantiated )
822
831
module . module . setExport ( 'default' , exports ) ;
823
- } else { // preemptively cache
832
+ } else {
833
+ // Preemptively cache
834
+ // We use a function to defer promise creation for async hooks.
824
835
ESMLoader . moduleMap . set (
825
836
url ,
826
- new ModuleJob ( ESMLoader , url , ( ) =>
837
+ // Module job creation will start promises.
838
+ // We make it a function to lazily trigger those promises
839
+ // for async hooks compatibility.
840
+ ( ) => new ModuleJob ( ESMLoader , url , ( ) =>
827
841
new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
828
842
this . setExport ( 'default' , exports ) ;
829
843
} )
830
- )
844
+ , false /* isMain */ , false /* inspectBrk */ )
831
845
) ;
832
846
}
833
847
}
@@ -856,20 +870,19 @@ Module.prototype.require = function(id) {
856
870
let resolvedArgv ;
857
871
let hasPausedEntry = false ;
858
872
859
- function wrapSafe ( filename , content ) {
873
+ function wrapSafe ( filename , content , cjsModuleInstance ) {
860
874
if ( patched ) {
861
875
const wrapper = Module . wrap ( content ) ;
862
876
return vm . runInThisContext ( wrapper , {
863
877
filename,
864
878
lineOffset : 0 ,
865
879
displayErrors : true ,
866
880
importModuleDynamically : experimentalModules ? async ( specifier ) => {
867
- const loader = await asyncESM . loaderPromise ;
881
+ const loader = asyncESM . ESMLoader ;
868
882
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
869
883
} : undefined ,
870
884
} ) ;
871
885
}
872
-
873
886
let compiled ;
874
887
try {
875
888
compiled = compileFunction (
@@ -890,17 +903,16 @@ function wrapSafe(filename, content) {
890
903
]
891
904
) ;
892
905
} catch ( err ) {
893
- if ( experimentalModules ) {
906
+ if ( experimentalModules && process . mainModule === cjsModuleInstance )
894
907
enrichCJSError ( err ) ;
895
- }
896
908
throw err ;
897
909
}
898
910
899
911
if ( experimentalModules ) {
900
912
const { callbackMap } = internalBinding ( 'module_wrap' ) ;
901
913
callbackMap . set ( compiled . cacheKey , {
902
914
importModuleDynamically : async ( specifier ) => {
903
- const loader = await asyncESM . loaderPromise ;
915
+ const loader = asyncESM . ESMLoader ;
904
916
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
905
917
}
906
918
} ) ;
@@ -923,7 +935,7 @@ Module.prototype._compile = function(content, filename) {
923
935
}
924
936
925
937
maybeCacheSourceMap ( filename , content , this ) ;
926
- const compiledWrapper = wrapSafe ( filename , content ) ;
938
+ const compiledWrapper = wrapSafe ( filename , content , this ) ;
927
939
928
940
var inspectorWrapper = null ;
929
941
if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
@@ -979,7 +991,11 @@ Module._extensions['.js'] = function(module, filename) {
979
991
'files in that package scope as ES modules.\nInstead rename ' +
980
992
`${ basename } to end in .cjs, change the requiring code to use ` +
981
993
'import(), or remove "type": "module" from ' +
982
- `${ path . resolve ( pkg . path , 'package.json' ) } .`
994
+ `${ path . resolve ( pkg . path , 'package.json' ) } .` ,
995
+ undefined ,
996
+ undefined ,
997
+ undefined ,
998
+ true
983
999
) ;
984
1000
warnRequireESM = false ;
985
1001
}
@@ -1022,26 +1038,15 @@ Module._extensions['.node'] = function(module, filename) {
1022
1038
return process . dlopen ( module , path . toNamespacedPath ( filename ) ) ;
1023
1039
} ;
1024
1040
1025
- Module . _extensions [ '.mjs' ] = function ( module , filename ) {
1026
- throw new ERR_REQUIRE_ESM ( filename ) ;
1027
- } ;
1028
-
1029
1041
// Bootstrap main module.
1030
- Module . runMain = function ( ) {
1031
- // Load the main module--the command line argument.
1032
- if ( experimentalModules ) {
1033
- asyncESM . loaderPromise . then ( ( loader ) => {
1034
- return loader . import ( pathToFileURL ( process . argv [ 1 ] ) . href ) ;
1035
- } )
1036
- . catch ( ( e ) => {
1037
- internalBinding ( 'errors' ) . triggerUncaughtException (
1038
- e ,
1039
- true /* fromPromise */
1040
- ) ;
1041
- } ) ;
1042
- return ;
1042
+ Module . runMain = function ( main = process . argv [ 1 ] ) {
1043
+ const resolvedMain = resolveMainPath ( main ) ;
1044
+ const useESMLoader = shouldUseESMLoader ( resolvedMain ) ;
1045
+ if ( useESMLoader ) {
1046
+ runMainESM ( resolvedMain || main ) ;
1047
+ } else {
1048
+ Module . _load ( main , null , true ) ;
1043
1049
}
1044
- Module . _load ( process . argv [ 1 ] , null , true ) ;
1045
1050
} ;
1046
1051
1047
1052
function createRequireFromPath ( filename ) {
@@ -1147,7 +1152,7 @@ Module.Module = Module;
1147
1152
1148
1153
// We have to load the esm things after module.exports!
1149
1154
if ( experimentalModules ) {
1150
- asyncESM = require ( 'internal/process/esm_loader' ) ;
1151
1155
ModuleJob = require ( 'internal/modules/esm/module_job' ) ;
1156
+ asyncESM = require ( 'internal/process/esm_loader' ) ;
1152
1157
( { ModuleWrap, kInstantiated } = internalBinding ( 'module_wrap' ) ) ;
1153
1158
}
0 commit comments