@@ -72,7 +72,8 @@ const cjsParseCache = new SafeWeakMap();
72
72
// Set first due to cycle with ESM loader functions.
73
73
module . exports = {
74
74
wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache,
75
- get hasLoadedAnyUserCJSModule ( ) { return hasLoadedAnyUserCJSModule ; }
75
+ get hasLoadedAnyUserCJSModule ( ) { return hasLoadedAnyUserCJSModule ; } ,
76
+ initializeCJS,
76
77
} ;
77
78
78
79
const { BuiltinModule } = require ( 'internal/bootstrap/loaders' ) ;
@@ -86,8 +87,8 @@ const {
86
87
kEmptyObject,
87
88
filterOwnProperties,
88
89
setOwnProperty,
90
+ getLazy,
89
91
} = require ( 'internal/util' ) ;
90
- const { Script } = require ( 'vm' ) ;
91
92
const { internalCompileFunction } = require ( 'internal/vm' ) ;
92
93
const assert = require ( 'internal/assert' ) ;
93
94
const fs = require ( 'fs' ) ;
@@ -97,21 +98,24 @@ const { sep } = path;
97
98
const { internalModuleStat } = internalBinding ( 'fs' ) ;
98
99
const { safeGetenv } = internalBinding ( 'credentials' ) ;
99
100
const {
100
- cjsConditions,
101
+ getCjsConditions,
102
+ initializeCjsConditions,
101
103
hasEsmSyntax,
102
104
loadBuiltinModule,
103
105
makeRequireFunction,
104
106
normalizeReferrerURL,
105
107
stripBOM,
106
108
} = require ( 'internal/modules/helpers' ) ;
107
- const { getOptionValue } = require ( 'internal/options' ) ;
108
- const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
109
- const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
110
- const shouldReportRequiredModules = process . env . WATCH_REPORT_DEPENDENCIES ;
111
- // Do not eagerly grab .manifest, it may be in TDZ
112
- const policy = getOptionValue ( '--experimental-policy' ) ?
113
- require ( 'internal/process/policy' ) :
114
- null ;
109
+ const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
110
+ const { getOptionValue, getEmbedderOptions } = require ( 'internal/options' ) ;
111
+ const policy = getLazy (
112
+ ( ) => ( getOptionValue ( '--experimental-policy' ) ? require ( 'internal/process/policy' ) : null )
113
+ ) ;
114
+ const shouldReportRequiredModules = getLazy ( ( ) => process . env . WATCH_REPORT_DEPENDENCIES ) ;
115
+
116
+ const getCascadedLoader = getLazy (
117
+ ( ) => require ( 'internal/process/esm_loader' ) . esmLoader
118
+ ) ;
115
119
116
120
// Whether any user-provided CJS modules had been loaded (executed).
117
121
// Used for internal assertions.
@@ -127,7 +131,6 @@ const {
127
131
setArrowMessage,
128
132
} = require ( 'internal/errors' ) ;
129
133
const { validateString } = require ( 'internal/validators' ) ;
130
- const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
131
134
132
135
const {
133
136
CHAR_BACKWARD_SLASH ,
@@ -140,15 +143,7 @@ const {
140
143
isProxy
141
144
} = require ( 'internal/util/types' ) ;
142
145
143
- const asyncESM = require ( 'internal/process/esm_loader' ) ;
144
- const { enrichCJSError } = require ( 'internal/modules/esm/translators' ) ;
145
146
const { kEvaluated } = internalBinding ( 'module_wrap' ) ;
146
- const {
147
- encodedSepRegEx,
148
- packageExportsResolve,
149
- packageImportsResolve
150
- } = require ( 'internal/modules/esm/resolve' ) ;
151
-
152
147
const isWindows = process . platform === 'win32' ;
153
148
154
149
const relativeResolveCache = ObjectCreate ( null ) ;
@@ -190,13 +185,13 @@ function updateChildren(parent, child, scan) {
190
185
}
191
186
192
187
function reportModuleToWatchMode ( filename ) {
193
- if ( shouldReportRequiredModules && process . send ) {
188
+ if ( shouldReportRequiredModules ( ) && process . send ) {
194
189
process . send ( { 'watch:require' : [ filename ] } ) ;
195
190
}
196
191
}
197
192
198
193
function reportModuleNotFoundToWatchMode ( basePath , extensions ) {
199
- if ( shouldReportRequiredModules && process . send ) {
194
+ if ( shouldReportRequiredModules ( ) && process . send ) {
200
195
process . send ( { 'watch:require' : ArrayPrototypeMap ( extensions , ( ext ) => path . resolve ( `${ basePath } ${ ext } ` ) ) } ) ;
201
196
}
202
197
}
@@ -213,22 +208,6 @@ function Module(id = '', parent) {
213
208
this . children = [ ] ;
214
209
}
215
210
216
- const builtinModules = [ ] ;
217
- for ( const { 0 : id , 1 : mod } of BuiltinModule . map ) {
218
- if ( mod . canBeRequiredByUsers &&
219
- BuiltinModule . canBeRequiredWithoutScheme ( id ) ) {
220
- ArrayPrototypePush ( builtinModules , id ) ;
221
- }
222
- }
223
-
224
- const allBuiltins = new SafeSet (
225
- ArrayPrototypeFlatMap ( builtinModules , ( bm ) => [ bm , `node:${ bm } ` ] )
226
- ) ;
227
- BuiltinModule . getSchemeOnlyModuleNames ( ) . forEach ( ( builtin ) => allBuiltins . add ( `node:${ builtin } ` ) ) ;
228
-
229
- ObjectFreeze ( builtinModules ) ;
230
- Module . builtinModules = builtinModules ;
231
-
232
211
Module . _cache = ObjectCreate ( null ) ;
233
212
Module . _pathCache = ObjectCreate ( null ) ;
234
213
Module . _extensions = ObjectCreate ( null ) ;
@@ -297,26 +276,59 @@ function setModuleParent(value) {
297
276
moduleParentCache . set ( this , value ) ;
298
277
}
299
278
300
- ObjectDefineProperty ( Module . prototype , 'parent' , {
301
- __proto__ : null ,
302
- get : pendingDeprecation ? deprecate (
303
- getModuleParent ,
304
- 'module.parent is deprecated due to accuracy issues. Please use ' +
305
- 'require.main to find program entry point instead.' ,
306
- 'DEP0144'
307
- ) : getModuleParent ,
308
- set : pendingDeprecation ? deprecate (
309
- setModuleParent ,
310
- 'module.parent is deprecated due to accuracy issues. Please use ' +
311
- 'require.main to find program entry point instead.' ,
312
- 'DEP0144'
313
- ) : setModuleParent ,
314
- } ) ;
315
-
316
279
let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'module' , ( fn ) => {
317
280
debug = fn ;
318
281
} ) ;
319
- Module . _debug = deprecate ( debug , 'Module._debug is deprecated.' , 'DEP0077' ) ;
282
+
283
+ const builtinModules = [ ] ;
284
+ // This function is called during pre-execution, before any user code is run.
285
+ function initializeCJS ( ) {
286
+ const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
287
+ ObjectDefineProperty ( Module . prototype , 'parent' , {
288
+ __proto__ : null ,
289
+ get : pendingDeprecation ? deprecate (
290
+ getModuleParent ,
291
+ 'module.parent is deprecated due to accuracy issues. Please use ' +
292
+ 'require.main to find program entry point instead.' ,
293
+ 'DEP0144'
294
+ ) : getModuleParent ,
295
+ set : pendingDeprecation ? deprecate (
296
+ setModuleParent ,
297
+ 'module.parent is deprecated due to accuracy issues. Please use ' +
298
+ 'require.main to find program entry point instead.' ,
299
+ 'DEP0144'
300
+ ) : setModuleParent ,
301
+ } ) ;
302
+ Module . _debug = deprecate ( debug , 'Module._debug is deprecated.' , 'DEP0077' ) ;
303
+
304
+ for ( const { 0 : id , 1 : mod } of BuiltinModule . map ) {
305
+ if ( mod . canBeRequiredByUsers &&
306
+ BuiltinModule . canBeRequiredWithoutScheme ( id ) ) {
307
+ ArrayPrototypePush ( builtinModules , id ) ;
308
+ }
309
+ }
310
+
311
+ const allBuiltins = new SafeSet (
312
+ ArrayPrototypeFlatMap ( builtinModules , ( bm ) => [ bm , `node:${ bm } ` ] )
313
+ ) ;
314
+ BuiltinModule . getSchemeOnlyModuleNames ( ) . forEach ( ( builtin ) => allBuiltins . add ( `node:${ builtin } ` ) ) ;
315
+ ObjectFreeze ( builtinModules ) ;
316
+ Module . builtinModules = builtinModules ;
317
+
318
+ Module . isBuiltin = function isBuiltin ( moduleName ) {
319
+ return allBuiltins . has ( moduleName ) ;
320
+ } ;
321
+
322
+ initializeCjsConditions ( ) ;
323
+
324
+ if ( ! getEmbedderOptions ( ) . noGlobalSearchPaths ) {
325
+ Module . _initPaths ( ) ;
326
+ }
327
+
328
+ // TODO(joyeecheung): deprecate this in favor of a proper hook?
329
+ Module . runMain =
330
+ require ( 'internal/modules/run_main' ) . executeUserEntryPoint ;
331
+ }
320
332
321
333
// Given a module name, and a list of paths to test, returns the first
322
334
// matching file in the following precedence.
@@ -337,7 +349,6 @@ function readPackage(requestPath) {
337
349
const existing = packageJsonCache . get ( jsonPath ) ;
338
350
if ( existing !== undefined ) return existing ;
339
351
340
- const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
341
352
const result = packageJsonReader . read ( jsonPath ) ;
342
353
const json = result . containsKeys === false ? '{}' : result . string ;
343
354
if ( json === undefined ) {
@@ -440,7 +451,7 @@ const realpathCache = new SafeMap();
440
451
function tryFile ( requestPath , isMain ) {
441
452
const rc = _stat ( requestPath ) ;
442
453
if ( rc !== 0 ) return ;
443
- if ( preserveSymlinks && ! isMain ) {
454
+ if ( getOptionValue ( '--preserve-symlinks' ) && ! isMain ) {
444
455
return path . resolve ( requestPath ) ;
445
456
}
446
457
return toRealPath ( requestPath ) ;
@@ -511,9 +522,10 @@ function trySelf(parentPath, request) {
511
522
}
512
523
513
524
try {
525
+ const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
514
526
return finalizeEsmResolution ( packageExportsResolve (
515
527
pathToFileURL ( pkgPath + '/package.json' ) , expansion , pkg ,
516
- pathToFileURL ( parentPath ) , cjsConditions ) , parentPath , pkgPath ) ;
528
+ pathToFileURL ( parentPath ) , getCjsConditions ( ) ) , parentPath , pkgPath ) ;
517
529
} catch ( e ) {
518
530
if ( e . code === 'ERR_MODULE_NOT_FOUND' )
519
531
throw createEsmNotFoundErr ( request , pkgPath + '/package.json' ) ;
@@ -535,9 +547,10 @@ function resolveExports(nmPath, request) {
535
547
const pkg = _readPackage ( pkgPath ) ;
536
548
if ( pkg ?. exports != null ) {
537
549
try {
550
+ const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
538
551
return finalizeEsmResolution ( packageExportsResolve (
539
552
pathToFileURL ( pkgPath + '/package.json' ) , '.' + expansion , pkg , null ,
540
- cjsConditions ) , null , pkgPath ) ;
553
+ getCjsConditions ( ) ) , null , pkgPath ) ;
541
554
} catch ( e ) {
542
555
if ( e . code === 'ERR_MODULE_NOT_FOUND' )
543
556
throw createEsmNotFoundErr ( request , pkgPath + '/package.json' ) ;
@@ -616,19 +629,19 @@ Module._findPath = function(request, paths, isMain) {
616
629
if ( ! trailingSlash ) {
617
630
if ( rc === 0 ) { // File.
618
631
if ( ! isMain ) {
619
- if ( preserveSymlinks ) {
632
+ if ( getOptionValue ( '--preserve-symlinks' ) ) {
620
633
filename = path . resolve ( basePath ) ;
621
634
} else {
622
635
filename = toRealPath ( basePath ) ;
623
636
}
624
- } else if ( preserveSymlinksMain ) {
625
- // For the main module, we use the preserveSymlinksMain flag instead
637
+ } else if ( getOptionValue ( '--preserve-symlinks-main' ) ) {
638
+ // For the main module, we use the --preserve-symlinks-main flag instead
626
639
// mainly for backward compatibility, as the preserveSymlinks flag
627
640
// historically has not applied to the main module. Most likely this
628
641
// was intended to keep .bin/ binaries working, as following those
629
642
// symlinks is usually required for the imports in the corresponding
630
643
// files to resolve; that said, in some use cases following symlinks
631
- // causes bigger problems which is why the preserveSymlinksMain option
644
+ // causes bigger problems which is why the --preserve-symlinks-main option
632
645
// is needed.
633
646
filename = path . resolve ( basePath ) ;
634
647
} else {
@@ -999,9 +1012,10 @@ Module._resolveFilename = function(request, parent, isMain, options) {
999
1012
const pkg = readPackageScope ( parentPath ) || { } ;
1000
1013
if ( pkg . data ?. imports != null ) {
1001
1014
try {
1015
+ const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
1002
1016
return finalizeEsmResolution (
1003
1017
packageImportsResolve ( request , pathToFileURL ( parentPath ) ,
1004
- cjsConditions ) , parentPath ,
1018
+ getCjsConditions ( ) ) , parentPath ,
1005
1019
pkg . path ) ;
1006
1020
} catch ( e ) {
1007
1021
if ( e . code === 'ERR_MODULE_NOT_FOUND' )
@@ -1043,6 +1057,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
1043
1057
} ;
1044
1058
1045
1059
function finalizeEsmResolution ( resolved , parentPath , pkgPath ) {
1060
+ const { encodedSepRegEx } = require ( 'internal/modules/esm/resolve' ) ;
1046
1061
if ( RegExpPrototypeExec ( encodedSepRegEx , resolved ) !== null )
1047
1062
throw new ERR_INVALID_MODULE_SPECIFIER (
1048
1063
resolved , 'must not include encoded "/" or "\\" characters' , parentPath ) ;
@@ -1081,14 +1096,14 @@ Module.prototype.load = function(filename) {
1081
1096
Module . _extensions [ extension ] ( this , filename ) ;
1082
1097
this . loaded = true ;
1083
1098
1084
- const esmLoader = asyncESM . esmLoader ;
1099
+ const cascadedLoader = getCascadedLoader ( ) ;
1085
1100
// Create module entry at load time to snapshot exports correctly
1086
1101
const exports = this . exports ;
1087
1102
// Preemptively cache
1088
1103
if ( ( module ?. module === undefined ||
1089
1104
module . module . getStatus ( ) < kEvaluated ) &&
1090
- ! esmLoader . cjsCache . has ( this ) )
1091
- esmLoader . cjsCache . set ( this , exports ) ;
1105
+ ! cascadedLoader . cjsCache . has ( this ) )
1106
+ cascadedLoader . cjsCache . set ( this , exports ) ;
1092
1107
} ;
1093
1108
1094
1109
@@ -1113,17 +1128,20 @@ Module.prototype.require = function(id) {
1113
1128
// (needed for setting breakpoint when called with --inspect-brk)
1114
1129
let resolvedArgv ;
1115
1130
let hasPausedEntry = false ;
1116
-
1131
+ let Script ;
1117
1132
function wrapSafe ( filename , content , cjsModuleInstance ) {
1118
1133
if ( patched ) {
1119
1134
const wrapper = Module . wrap ( content ) ;
1135
+ if ( Script === undefined ) {
1136
+ ( { Script } = require ( 'vm' ) ) ;
1137
+ }
1120
1138
const script = new Script ( wrapper , {
1121
1139
filename,
1122
1140
lineOffset : 0 ,
1123
1141
importModuleDynamically : async ( specifier , _ , importAssertions ) => {
1124
- const loader = asyncESM . esmLoader ;
1125
- return loader . import ( specifier , normalizeReferrerURL ( filename ) ,
1126
- importAssertions ) ;
1142
+ const cascadedLoader = getCascadedLoader ( ) ;
1143
+ return cascadedLoader . import ( specifier , normalizeReferrerURL ( filename ) ,
1144
+ importAssertions ) ;
1127
1145
} ,
1128
1146
} ) ;
1129
1147
@@ -1147,9 +1165,9 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1147
1165
] , {
1148
1166
filename,
1149
1167
importModuleDynamically ( specifier , _ , importAssertions ) {
1150
- const loader = asyncESM . esmLoader ;
1151
- return loader . import ( specifier , normalizeReferrerURL ( filename ) ,
1152
- importAssertions ) ;
1168
+ const cascadedLoader = getCascadedLoader ( ) ;
1169
+ return cascadedLoader . import ( specifier , normalizeReferrerURL ( filename ) ,
1170
+ importAssertions ) ;
1153
1171
} ,
1154
1172
} ) ;
1155
1173
@@ -1160,8 +1178,10 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1160
1178
1161
1179
return result . function ;
1162
1180
} catch ( err ) {
1163
- if ( process . mainModule === cjsModuleInstance )
1181
+ if ( process . mainModule === cjsModuleInstance ) {
1182
+ const { enrichCJSError } = require ( 'internal/modules/esm/translators' ) ;
1164
1183
enrichCJSError ( err , content ) ;
1184
+ }
1165
1185
throw err ;
1166
1186
}
1167
1187
}
@@ -1173,10 +1193,11 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1173
1193
Module . prototype . _compile = function ( content , filename ) {
1174
1194
let moduleURL ;
1175
1195
let redirects ;
1176
- if ( policy ?. manifest ) {
1196
+ const manifest = policy ( ) ?. manifest ;
1197
+ if ( manifest ) {
1177
1198
moduleURL = pathToFileURL ( filename ) ;
1178
- redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
1179
- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1199
+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
1200
+ manifest . assertIntegrity ( moduleURL , content ) ;
1180
1201
}
1181
1202
1182
1203
const compiledWrapper = wrapSafe ( filename , content , this ) ;
@@ -1277,9 +1298,10 @@ Module._extensions['.js'] = function(module, filename) {
1277
1298
Module . _extensions [ '.json' ] = function ( module , filename ) {
1278
1299
const content = fs . readFileSync ( filename , 'utf8' ) ;
1279
1300
1280
- if ( policy ?. manifest ) {
1301
+ const manifest = policy ( ) ?. manifest ;
1302
+ if ( manifest ) {
1281
1303
const moduleURL = pathToFileURL ( filename ) ;
1282
- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1304
+ manifest . assertIntegrity ( moduleURL , content ) ;
1283
1305
}
1284
1306
1285
1307
try {
@@ -1293,10 +1315,11 @@ Module._extensions['.json'] = function(module, filename) {
1293
1315
1294
1316
// Native extension for .node
1295
1317
Module . _extensions [ '.node' ] = function ( module , filename ) {
1296
- if ( policy ?. manifest ) {
1318
+ const manifest = policy ( ) ?. manifest ;
1319
+ if ( manifest ) {
1297
1320
const content = fs . readFileSync ( filename ) ;
1298
1321
const moduleURL = pathToFileURL ( filename ) ;
1299
- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1322
+ manifest . assertIntegrity ( moduleURL , content ) ;
1300
1323
}
1301
1324
// Be aware this doesn't use `content`
1302
1325
return process . dlopen ( module , path . toNamespacedPath ( filename ) ) ;
@@ -1405,9 +1428,5 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
1405
1428
}
1406
1429
} ;
1407
1430
1408
- Module . isBuiltin = function isBuiltin ( moduleName ) {
1409
- return allBuiltins . has ( moduleName ) ;
1410
- } ;
1411
-
1412
1431
// Backwards compatibility
1413
1432
Module . Module = Module ;
0 commit comments