@@ -155,6 +155,11 @@ let requireDepth = 0;
155
155
let isPreloading = false ;
156
156
let statCache = null ;
157
157
158
+ /**
159
+ * Our internal implementation of `require`.
160
+ * @param {Module } module Parent module of what is being required
161
+ * @param {string } id Specifier of the child module being imported
162
+ */
158
163
function internalRequire ( module , id ) {
159
164
validateString ( id , 'id' ) ;
160
165
if ( id === '' ) {
@@ -169,6 +174,10 @@ function internalRequire(module, id) {
169
174
}
170
175
}
171
176
177
+ /**
178
+ * Get a path's properties, using an in-memory cache to minimize lookups.
179
+ * @param {string } filename Absolute path to the file
180
+ */
172
181
function stat ( filename ) {
173
182
filename = path . toNamespacedPath ( filename ) ;
174
183
if ( statCache !== null ) {
@@ -195,24 +204,45 @@ ObjectDefineProperty(Module, '_stat', {
195
204
configurable : true ,
196
205
} ) ;
197
206
207
+ /**
208
+ * Update the parent's children array with the child module.
209
+ * @param {Module } parent Module requiring the children
210
+ * @param {Module } child Module being required
211
+ * @param {boolean } scan Add the child to the parent's children if not already present
212
+ */
198
213
function updateChildren ( parent , child , scan ) {
199
214
const children = parent ?. children ;
200
215
if ( children && ! ( scan && ArrayPrototypeIncludes ( children , child ) ) ) { ArrayPrototypePush ( children , child ) ; }
201
216
}
202
217
218
+ /**
219
+ * Tell the watch mode that a module was required.
220
+ * @param {string } filename Absolute path of the module
221
+ */
203
222
function reportModuleToWatchMode ( filename ) {
204
223
if ( shouldReportRequiredModules ( ) && process . send ) {
205
224
process . send ( { 'watch:require' : [ filename ] } ) ;
206
225
}
207
226
}
208
227
228
+ /**
229
+ * Tell the watch mode that a module was not found.
230
+ * @param {string } basePath The absolute path that errored
231
+ * @param {string[] } extensions The extensions that were tried
232
+ */
209
233
function reportModuleNotFoundToWatchMode ( basePath , extensions ) {
210
234
if ( shouldReportRequiredModules ( ) && process . send ) {
211
235
process . send ( { 'watch:require' : ArrayPrototypeMap ( extensions , ( ext ) => path . resolve ( `${ basePath } ${ ext } ` ) ) } ) ;
212
236
}
213
237
}
214
238
239
+ /** @type {Map<Module, Module> } */
215
240
const moduleParentCache = new SafeWeakMap ( ) ;
241
+ /**
242
+ * Create a new module instance.
243
+ * @param {string } id
244
+ * @param {Module } parent
245
+ */
216
246
function Module ( id = '' , parent ) {
217
247
this . id = id ;
218
248
this . path = path . dirname ( id ) ;
@@ -235,16 +265,24 @@ function Module(id = '', parent) {
235
265
this [ require_private_symbol ] = internalRequire ;
236
266
}
237
267
268
+ /** @type {Record<string, Module> } */
238
269
Module . _cache = { __proto__ : null } ;
270
+ /** @type {Record<string, string> } */
239
271
Module . _pathCache = { __proto__ : null } ;
272
+ /** @type {Record<string, (module: Module, filename: string) => void> } */
240
273
Module . _extensions = { __proto__ : null } ;
274
+ /** @type {string[] } */
241
275
let modulePaths = [ ] ;
276
+ /** @type {string[] } */
242
277
Module . globalPaths = [ ] ;
243
278
244
279
let patched = false ;
245
280
246
- // eslint-disable-next-line func-style
247
- let wrap = function ( script ) {
281
+ /**
282
+ * Add the CommonJS wrapper around a module's source code.
283
+ * @param {string } script Module source code
284
+ */
285
+ let wrap = function ( script ) { // eslint-disable-line func-style
248
286
return Module . wrapper [ 0 ] + script + Module . wrapper [ 1 ] ;
249
287
} ;
250
288
@@ -295,10 +333,17 @@ const isPreloadingDesc = { get() { return isPreloading; } };
295
333
ObjectDefineProperty ( Module . prototype , 'isPreloading' , isPreloadingDesc ) ;
296
334
ObjectDefineProperty ( BuiltinModule . prototype , 'isPreloading' , isPreloadingDesc ) ;
297
335
336
+ /**
337
+ * Get the parent of the current module from our cache.
338
+ */
298
339
function getModuleParent ( ) {
299
340
return moduleParentCache . get ( this ) ;
300
341
}
301
342
343
+ /**
344
+ * Set the parent of the current module in our cache.
345
+ * @param {Module } value
346
+ */
302
347
function setModuleParent ( value ) {
303
348
moduleParentCache . set ( this , value ) ;
304
349
}
@@ -325,7 +370,10 @@ ObjectDefineProperty(Module.prototype, 'parent', {
325
370
Module . _debug = pendingDeprecate ( debug , 'Module._debug is deprecated.' , 'DEP0077' ) ;
326
371
Module . isBuiltin = BuiltinModule . isBuiltin ;
327
372
328
- // This function is called during pre-execution, before any user code is run.
373
+ /**
374
+ * Prepare to run CommonJS code.
375
+ * This function is called during pre-execution, before any user code is run.
376
+ */
329
377
function initializeCJS ( ) {
330
378
// This need to be done at runtime in case --expose-internals is set.
331
379
const builtinModules = BuiltinModule . getCanBeRequiredByUsersWithoutSchemeList ( ) ;
@@ -373,6 +421,11 @@ ObjectDefineProperty(Module, '_readPackage', {
373
421
configurable : true ,
374
422
} ) ;
375
423
424
+ /**
425
+ * Get the nearest parent package.json file from a given path.
426
+ * Return the package.json data and the path to the package.json file, or false.
427
+ * @param {string } checkPath The path to start searching from.
428
+ */
376
429
function readPackageScope ( checkPath ) {
377
430
const rootSeparatorIndex = StringPrototypeIndexOf ( checkPath , sep ) ;
378
431
let separatorIndex ;
@@ -397,6 +450,13 @@ function readPackageScope(checkPath) {
397
450
return false ;
398
451
}
399
452
453
+ /**
454
+ * Try to load a specifier as a package.
455
+ * @param {string } requestPath The path to what we are trying to load
456
+ * @param {string[] } exts File extensions to try appending in order to resolve the file
457
+ * @param {boolean } isMain Whether the file is the main entry point of the app
458
+ * @param {string } originalPath The specifier passed to `require`
459
+ */
400
460
function tryPackage ( requestPath , exts , isMain , originalPath ) {
401
461
const pkg = _readPackage ( requestPath ) . main ;
402
462
@@ -434,15 +494,20 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
434
494
return actual ;
435
495
}
436
496
437
- // In order to minimize unnecessary lstat() calls,
438
- // this cache is a list of known-real paths.
439
- // Set to an empty Map to reset.
497
+ /**
498
+ * Cache for storing resolved real paths of modules.
499
+ * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths.
500
+ * Set to an empty Map to reset.
501
+ * @type {Map<string, string> }
502
+ */
440
503
const realpathCache = new SafeMap ( ) ;
441
504
442
- // Check if the file exists and is not a directory
443
- // if using --preserve-symlinks and isMain is false,
444
- // keep symlinks intact, otherwise resolve to the
445
- // absolute realpath.
505
+ /**
506
+ * Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false, keep symlinks
507
+ * intact, otherwise resolve to the absolute realpath.
508
+ * @param {string } requestPath The path to the file to load.
509
+ * @param {boolean } isMain Whether the file is the main module.
510
+ */
446
511
function tryFile ( requestPath , isMain ) {
447
512
const rc = _stat ( requestPath ) ;
448
513
if ( rc !== 0 ) { return ; }
@@ -452,16 +517,26 @@ function tryFile(requestPath, isMain) {
452
517
return toRealPath ( requestPath ) ;
453
518
}
454
519
520
+
521
+ /**
522
+ * Resolves the path of a given `require` specifier, following symlinks.
523
+ * @param {string } requestPath The `require` specifier
524
+ */
455
525
function toRealPath ( requestPath ) {
456
526
return fs . realpathSync ( requestPath , {
457
527
[ internalFS . realpathCacheKey ] : realpathCache ,
458
528
} ) ;
459
529
}
460
530
461
- // Given a path, check if the file exists with any of the set extensions
462
- function tryExtensions ( p , exts , isMain ) {
531
+ /**
532
+ * Given a path, check if the file exists with any of the set extensions.
533
+ * @param {string } basePath The path and filename without extension
534
+ * @param {string[] } exts The extensions to try
535
+ * @param {boolean } isMain Whether the module is the main module
536
+ */
537
+ function tryExtensions ( basePath , exts , isMain ) {
463
538
for ( let i = 0 ; i < exts . length ; i ++ ) {
464
- const filename = tryFile ( p + exts [ i ] , isMain ) ;
539
+ const filename = tryFile ( basePath + exts [ i ] , isMain ) ;
465
540
466
541
if ( filename ) {
467
542
return filename ;
@@ -470,8 +545,10 @@ function tryExtensions(p, exts, isMain) {
470
545
return false ;
471
546
}
472
547
473
- // Find the longest (possibly multi-dot) extension registered in
474
- // Module._extensions
548
+ /**
549
+ * Find the longest (possibly multi-dot) extension registered in `Module._extensions`.
550
+ * @param {string } filename The filename to find the longest registered extension for.
551
+ */
475
552
function findLongestRegisteredExtension ( filename ) {
476
553
const name = path . basename ( filename ) ;
477
554
let currentExtension ;
@@ -486,6 +563,10 @@ function findLongestRegisteredExtension(filename) {
486
563
return '.js' ;
487
564
}
488
565
566
+ /**
567
+ * Tries to get the absolute file path of the parent module.
568
+ * @param {Module } parent The parent module object.
569
+ */
489
570
function trySelfParentPath ( parent ) {
490
571
if ( ! parent ) { return false ; }
491
572
@@ -500,6 +581,11 @@ function trySelfParentPath(parent) {
500
581
}
501
582
}
502
583
584
+ /**
585
+ * Attempt to resolve a module request using the parent module package metadata.
586
+ * @param {string } parentPath The path of the parent module
587
+ * @param {string } request The module request to resolve
588
+ */
503
589
function trySelf ( parentPath , request ) {
504
590
if ( ! parentPath ) { return false ; }
505
591
@@ -528,10 +614,18 @@ function trySelf(parentPath, request) {
528
614
}
529
615
}
530
616
531
- // This only applies to requests of a specific form:
532
- // 1. name/.*
533
- // 2. @scope/name/.*
617
+ /**
618
+ * This only applies to requests of a specific form:
619
+ * 1. `name/.*`
620
+ * 2. `@scope/name/.*`
621
+ */
534
622
const EXPORTS_PATTERN = / ^ ( (?: @ [ ^ / \\ % ] + \/ ) ? [ ^ . / \\ % ] [ ^ / \\ % ] * ) ( \/ .* ) ? $ / ;
623
+
624
+ /**
625
+ * Resolves the exports for a given module path and request.
626
+ * @param {string } nmPath The path to the module.
627
+ * @param {string } request The request for the module.
628
+ */
535
629
function resolveExports ( nmPath , request ) {
536
630
// The implementation's behavior is meant to mirror resolution in ESM.
537
631
const { 1 : name , 2 : expansion = '' } =
@@ -553,9 +647,10 @@ function resolveExports(nmPath, request) {
553
647
}
554
648
555
649
/**
556
- * @param {string } request a relative or absolute file path
557
- * @param {Array<string> } paths file system directories to search as file paths
558
- * @param {boolean } isMain if the request is the main app entry point
650
+ * Get the absolute path to a module.
651
+ * @param {string } request Relative or absolute file path
652
+ * @param {Array<string> } paths Folders to search as file paths
653
+ * @param {boolean } isMain Whether the request is the main app entry point
559
654
* @returns {string | false }
560
655
*/
561
656
Module . _findPath = function ( request , paths , isMain ) {
@@ -673,11 +768,14 @@ Module._findPath = function(request, paths, isMain) {
673
768
return false ;
674
769
} ;
675
770
676
- // ' node_modules' character codes reversed
771
+ /** ` node_modules` character codes reversed */
677
772
const nmChars = [ 115 , 101 , 108 , 117 , 100 , 111 , 109 , 95 , 101 , 100 , 111 , 110 ] ;
678
773
const nmLen = nmChars . length ;
679
774
if ( isWindows ) {
680
- // 'from' is the __dirname of the module.
775
+ /**
776
+ * Get the paths to the `node_modules` folder for a given path.
777
+ * @param {string } from `__dirname` of the module
778
+ */
681
779
Module . _nodeModulePaths = function ( from ) {
682
780
// Guarantee that 'from' is absolute.
683
781
from = path . resolve ( from ) ;
@@ -692,6 +790,7 @@ if (isWindows) {
692
790
CHAR_BACKWARD_SLASH &&
693
791
StringPrototypeCharCodeAt ( from , from . length - 2 ) === CHAR_COLON ) { return [ from + 'node_modules' ] ; }
694
792
793
+ /** @type {string[] } */
695
794
const paths = [ ] ;
696
795
for ( let i = from . length - 1 , p = 0 , last = from . length ; i >= 0 ; -- i ) {
697
796
const code = StringPrototypeCharCodeAt ( from , i ) ;
@@ -723,7 +822,10 @@ if (isWindows) {
723
822
return paths ;
724
823
} ;
725
824
} else { // posix
726
- // 'from' is the __dirname of the module.
825
+ /**
826
+ * Get the paths to the `node_modules` folder for a given path.
827
+ * @param {string } from `__dirname` of the module
828
+ */
727
829
Module . _nodeModulePaths = function ( from ) {
728
830
// Guarantee that 'from' is absolute.
729
831
from = path . resolve ( from ) ;
@@ -734,6 +836,7 @@ if (isWindows) {
734
836
// note: this approach *only* works when the path is guaranteed
735
837
// to be absolute. Doing a fully-edge-case-correct path.split
736
838
// that works on both Windows and Posix is non-trivial.
839
+ /** @type {string[] } */
737
840
const paths = [ ] ;
738
841
for ( let i = from . length - 1 , p = 0 , last = from . length ; i >= 0 ; -- i ) {
739
842
const code = StringPrototypeCharCodeAt ( from , i ) ;
@@ -762,6 +865,11 @@ if (isWindows) {
762
865
} ;
763
866
}
764
867
868
+ /**
869
+ * Get the paths for module resolution.
870
+ * @param {string } request
871
+ * @param {Module } parent
872
+ */
765
873
Module . _resolveLookupPaths = function ( request , parent ) {
766
874
if ( BuiltinModule . normalizeRequirableId ( request ) ) {
767
875
debug ( 'looking for %j in []' , request ) ;
@@ -775,6 +883,7 @@ Module._resolveLookupPaths = function(request, parent) {
775
883
StringPrototypeCharAt ( request , 1 ) !== '/' &&
776
884
( ! isWindows || StringPrototypeCharAt ( request , 1 ) !== '\\' ) ) ) {
777
885
886
+ /** @type {string[] } */
778
887
let paths ;
779
888
if ( parent ?. paths ?. length ) {
780
889
paths = ArrayPrototypeSlice ( modulePaths ) ;
@@ -804,6 +913,10 @@ Module._resolveLookupPaths = function(request, parent) {
804
913
return parentDir ;
805
914
} ;
806
915
916
+ /**
917
+ * Emits a warning when a non-existent property of module exports is accessed inside a circular dependency.
918
+ * @param {string } prop The name of the non-existent property.
919
+ */
807
920
function emitCircularRequireWarning ( prop ) {
808
921
process . emitWarning (
809
922
`Accessing non-existent property '${ String ( prop ) } ' of module exports ` +
@@ -834,6 +947,12 @@ const CircularRequirePrototypeWarningProxy = new Proxy({}, {
834
947
} ,
835
948
} ) ;
836
949
950
+ /**
951
+ * Returns the exports object for a module that has a circular `require`.
952
+ * If the exports object is a plain object, it is wrapped in a proxy that warns
953
+ * about circular dependencies.
954
+ * @param {Module } module The module instance
955
+ */
837
956
function getExportsForCircularRequire ( module ) {
838
957
if ( module . exports &&
839
958
! isProxy ( module . exports ) &&
@@ -851,13 +970,17 @@ function getExportsForCircularRequire(module) {
851
970
return module . exports ;
852
971
}
853
972
854
- // Check the cache for the requested file.
855
- // 1. If a module already exists in the cache: return its exports object.
856
- // 2. If the module is native: call
857
- // `BuiltinModule.prototype.compileForPublicLoader()` and return the exports.
858
- // 3. Otherwise, create a new module for the file and save it to the cache.
859
- // Then have it load the file contents before returning its exports
860
- // object.
973
+ /**
974
+ * Load a module from cache if it exists, otherwise create a new module instance.
975
+ * 1. If a module already exists in the cache: return its exports object.
976
+ * 2. If the module is native: call
977
+ * `BuiltinModule.prototype.compileForPublicLoader()` and return the exports.
978
+ * 3. Otherwise, create a new module for the file and save it to the cache.
979
+ * Then have it load the file contents before returning its exports object.
980
+ * @param {string } request Specifier of module to load via `require`
981
+ * @param {string } parent Absolute path of the module importing the child
982
+ * @param {boolean } isMain Whether the module is the main entry point
983
+ */
861
984
Module . _load = function ( request , parent , isMain ) {
862
985
let relResolveCacheIdentifier ;
863
986
if ( parent ) {
@@ -953,6 +1076,15 @@ Module._load = function(request, parent, isMain) {
953
1076
return module . exports ;
954
1077
} ;
955
1078
1079
+ /**
1080
+ * Given a `require` string and its context, get its absolute file path.
1081
+ * @param {string } request The specifier to resolve
1082
+ * @param {Module } parent The module containing the `require` call
1083
+ * @param {boolean } isMain Whether the module is the main entry point
1084
+ * @param {ResolveFilenameOptions } options Options object
1085
+ * @typedef {object } ResolveFilenameOptions
1086
+ * @property {string[] } paths Paths to search for modules in
1087
+ */
956
1088
Module . _resolveFilename = function ( request , parent , isMain , options ) {
957
1089
if ( BuiltinModule . normalizeRequirableId ( request ) ) {
958
1090
return request ;
@@ -1041,6 +1173,14 @@ Module._resolveFilename = function(request, parent, isMain, options) {
1041
1173
throw err ;
1042
1174
} ;
1043
1175
1176
+ /**
1177
+ * Finishes resolving an ES module specifier into an absolute file path.
1178
+ * @param {string } resolved The resolved module specifier
1179
+ * @param {string } parentPath The path of the parent module
1180
+ * @param {string } pkgPath The path of the package.json file
1181
+ * @throws {ERR_INVALID_MODULE_SPECIFIER } If the resolved module specifier contains encoded `/` or `\\` characters
1182
+ * @throws {Error } If the module cannot be found
1183
+ */
1044
1184
function finalizeEsmResolution ( resolved , parentPath , pkgPath ) {
1045
1185
const { encodedSepRegEx } = require ( 'internal/modules/esm/resolve' ) ;
1046
1186
if ( RegExpPrototypeExec ( encodedSepRegEx , resolved ) !== null ) {
@@ -1055,6 +1195,11 @@ function finalizeEsmResolution(resolved, parentPath, pkgPath) {
1055
1195
throw err ;
1056
1196
}
1057
1197
1198
+ /**
1199
+ * Creates an error object for when a requested ES module cannot be found.
1200
+ * @param {string } request The name of the requested module
1201
+ * @param {string } [path] The path to the requested module
1202
+ */
1058
1203
function createEsmNotFoundErr ( request , path ) {
1059
1204
// eslint-disable-next-line no-restricted-syntax
1060
1205
const err = new Error ( `Cannot find module '${ request } '` ) ;
@@ -1064,7 +1209,10 @@ function createEsmNotFoundErr(request, path) {
1064
1209
return err ;
1065
1210
}
1066
1211
1067
- // Given a file name, pass it to the proper extension handler.
1212
+ /**
1213
+ * Given a file name, pass it to the proper extension handler.
1214
+ * @param {string } filename The `require` specifier
1215
+ */
1068
1216
Module . prototype . load = function ( filename ) {
1069
1217
debug ( 'load %j for module %j' , filename , this . id ) ;
1070
1218
@@ -1090,9 +1238,12 @@ Module.prototype.load = function(filename) {
1090
1238
! cascadedLoader . cjsCache . has ( this ) ) { cascadedLoader . cjsCache . set ( this , exports ) ; }
1091
1239
} ;
1092
1240
1093
- // Loads a module at the given file path. Returns that module's
1094
- // `exports` property.
1095
- // Note: when using the experimental policy mechanism this function is overridden
1241
+ /**
1242
+ * Loads a module at the given file path. Returns that module's `exports` property.
1243
+ * Note: when using the experimental policy mechanism this function is overridden.
1244
+ * @param {string } id
1245
+ * @throws {ERR_INVALID_ARG_TYPE } When `id` is not a string
1246
+ */
1096
1247
Module . prototype . require = function ( id ) {
1097
1248
validateString ( id , 'id' ) ;
1098
1249
if ( id === '' ) {
@@ -1107,11 +1258,23 @@ Module.prototype.require = function(id) {
1107
1258
}
1108
1259
} ;
1109
1260
1110
- // Resolved path to process.argv[1] will be lazily placed here
1111
- // (needed for setting breakpoint when called with --inspect-brk)
1261
+ /**
1262
+ * Resolved path to `process.argv[1]` will be lazily placed here
1263
+ * (needed for setting breakpoint when called with `--inspect-brk`).
1264
+ * @type {string | undefined }
1265
+ */
1112
1266
let resolvedArgv ;
1113
1267
let hasPausedEntry = false ;
1268
+ /** @type {import('vm').Script } */
1114
1269
let Script ;
1270
+
1271
+ /**
1272
+ * Wraps the given content in a script and runs it in a new context.
1273
+ * @param {string } filename The name of the file being loaded
1274
+ * @param {string } content The content of the file being loaded
1275
+ * @param {Module } cjsModuleInstance The CommonJS loader instance
1276
+ * @param {object } codeCache The SEA code cache
1277
+ */
1115
1278
function wrapSafe ( filename , content , cjsModuleInstance , codeCache ) {
1116
1279
if ( patched ) {
1117
1280
const wrapper = Module . wrap ( content ) ;
@@ -1177,10 +1340,12 @@ function wrapSafe(filename, content, cjsModuleInstance, codeCache) {
1177
1340
}
1178
1341
}
1179
1342
1180
- // Run the file contents in the correct scope or sandbox. Expose
1181
- // the correct helper variables (require, module, exports) to
1182
- // the file.
1183
- // Returns exception, if any.
1343
+ /**
1344
+ * Run the file contents in the correct scope or sandbox. Expose the correct helper variables (`require`, `module`,
1345
+ * `exports`) to the file. Returns exception, if any.
1346
+ * @param {string } content The source code of the module
1347
+ * @param {string } filename The file path of the module
1348
+ */
1184
1349
Module . prototype . _compile = function ( content , filename ) {
1185
1350
let moduleURL ;
1186
1351
let redirects ;
@@ -1235,7 +1400,11 @@ Module.prototype._compile = function(content, filename) {
1235
1400
return result ;
1236
1401
} ;
1237
1402
1238
- // Native extension for .js
1403
+ /**
1404
+ * Native handler for `.js` files.
1405
+ * @param {Module } module The module to compile
1406
+ * @param {string } filename The file path of the module
1407
+ */
1239
1408
Module . _extensions [ '.js' ] = function ( module , filename ) {
1240
1409
// If already analyzed the source, then it will be cached.
1241
1410
const cached = cjsParseCache . get ( module ) ;
@@ -1284,8 +1453,11 @@ Module._extensions['.js'] = function(module, filename) {
1284
1453
module . _compile ( content , filename ) ;
1285
1454
} ;
1286
1455
1287
-
1288
- // Native extension for .json
1456
+ /**
1457
+ * Native handler for `.json` files.
1458
+ * @param {Module } module The module to compile
1459
+ * @param {string } filename The file path of the module
1460
+ */
1289
1461
Module . _extensions [ '.json' ] = function ( module , filename ) {
1290
1462
const content = fs . readFileSync ( filename , 'utf8' ) ;
1291
1463
@@ -1303,8 +1475,11 @@ Module._extensions['.json'] = function(module, filename) {
1303
1475
}
1304
1476
} ;
1305
1477
1306
-
1307
- // Native extension for .node
1478
+ /**
1479
+ * Native handler for `.node` files.
1480
+ * @param {Module } module The module to compile
1481
+ * @param {string } filename The file path of the module
1482
+ */
1308
1483
Module . _extensions [ '.node' ] = function ( module , filename ) {
1309
1484
const manifest = policy ( ) ?. manifest ;
1310
1485
if ( manifest ) {
@@ -1316,6 +1491,10 @@ Module._extensions['.node'] = function(module, filename) {
1316
1491
return process . dlopen ( module , path . toNamespacedPath ( filename ) ) ;
1317
1492
} ;
1318
1493
1494
+ /**
1495
+ * Creates a `require` function that can be used to load modules from the specified path.
1496
+ * @param {string } filename The path to the module
1497
+ */
1319
1498
function createRequireFromPath ( filename ) {
1320
1499
// Allow a directory to be passed as the filename
1321
1500
const trailingSlash =
@@ -1336,6 +1515,12 @@ function createRequireFromPath(filename) {
1336
1515
const createRequireError = 'must be a file URL object, file URL string, or ' +
1337
1516
'absolute path string' ;
1338
1517
1518
+ /**
1519
+ * Creates a new `require` function that can be used to load modules.
1520
+ * @param {string | URL } filename The path or URL to the module context for this `require`
1521
+ * @throws {ERR_INVALID_ARG_VALUE } If `filename` is not a string or URL, or if it is a relative path that cannot be
1522
+ * resolved to an absolute path.
1523
+ */
1339
1524
function createRequire ( filename ) {
1340
1525
let filepath ;
1341
1526
@@ -1357,6 +1542,9 @@ function createRequire(filename) {
1357
1542
1358
1543
Module . createRequire = createRequire ;
1359
1544
1545
+ /**
1546
+ * Define the paths to use for resolving a module.
1547
+ */
1360
1548
Module . _initPaths = function ( ) {
1361
1549
const homeDir = isWindows ? process . env . USERPROFILE : safeGetenv ( 'HOME' ) ;
1362
1550
const nodePath = isWindows ? process . env . NODE_PATH : safeGetenv ( 'NODE_PATH' ) ;
@@ -1387,6 +1575,10 @@ Module._initPaths = function() {
1387
1575
Module . globalPaths = ArrayPrototypeSlice ( modulePaths ) ;
1388
1576
} ;
1389
1577
1578
+ /**
1579
+ * Handle modules loaded via `--require`.
1580
+ * @param {string[] } requests The values of `--require`
1581
+ */
1390
1582
Module . _preloadModules = function ( requests ) {
1391
1583
if ( ! ArrayIsArray ( requests ) ) { return ; }
1392
1584
@@ -1408,6 +1600,10 @@ Module._preloadModules = function(requests) {
1408
1600
isPreloading = false ;
1409
1601
} ;
1410
1602
1603
+ /**
1604
+ * If the user has overridden an export from a builtin module, this function can ensure that the override is used in
1605
+ * both CommonJS and ES module contexts.
1606
+ */
1411
1607
Module . syncBuiltinESMExports = function syncBuiltinESMExports ( ) {
1412
1608
for ( const mod of BuiltinModule . map . values ( ) ) {
1413
1609
if ( BuiltinModule . canBeRequiredWithoutScheme ( mod . id ) ) {
0 commit comments