@@ -91,8 +91,8 @@ By default, Node.js will treat the following as CommonJS modules:
91
91
interpreted.
92
92
93
93
* Files with an extension that is not ` .mjs ` , ` .cjs ` , ` .json ` , ` .node ` , or ` .js `
94
- ( when the nearest parent ` package.json ` file contains a top-level field
95
- [ ` "type" ` ] [ ] with a value of ` "module" ` , those files will be recognized as
94
+ when the nearest parent ` package.json ` file contains a top-level field
95
+ [ ` "type" ` ] [ ] with a value of ` "module" ` ( those files will be recognized as
96
96
CommonJS modules only if they are being included via ` require() ` , not when
97
97
used as the command-line entry point of the program).
98
98
@@ -109,7 +109,7 @@ When a file is run directly from Node.js, `require.main` is set to its
109
109
` module ` . That means that it is possible to determine whether a file has been
110
110
run directly by testing ` require.main === module ` .
111
111
112
- For a file ` foo.js ` , this will be ` true ` if run via ` node foo.js ` , but
112
+ For example given a file ` foo.js ` , this will be ` true ` if run via ` node foo.js ` , but
113
113
` false ` if run by ` require('./foo') ` .
114
114
115
115
When the entry point is not a CommonJS module, ` require.main ` is ` undefined ` ,
@@ -208,8 +208,8 @@ regarding which files are parsed as ECMAScript modules.
208
208
3 . The file has a ` .js ` extension, the closest ` package.json ` does not contain
209
209
` "type": "commonjs" ` , and the module contains ES module syntax.
210
210
211
- If the ES Module being loaded meet the requirements, ` require() ` can load it and
212
- return the module namespace object. In this case it is similar to dynamic
211
+ If the ES Module being loaded meets the requirements, ` require() ` can load it and
212
+ return the [ module namespace object] [ ] . In this case it is similar to dynamic
213
213
` import() ` but is run synchronously and returns the name space object
214
214
directly.
215
215
@@ -253,8 +253,8 @@ This property is experimental and can change in the future. It should only be us
253
253
by tools converting ES modules into CommonJS modules, following existing ecosystem
254
254
conventions. Code authored directly in CommonJS should avoid depending on it.
255
255
256
- When a ES Module contains both named exports and a default export, the result returned by ` require() `
257
- is the module namespace object, which places the default export in the ` .default ` property, similar to
256
+ When an ES Module contains both named exports and a default export, the result returned by ` require() `
257
+ is the [ module namespace object] [ ] , which places the default export in the ` .default ` property, similar to
258
258
the results returned by ` import() ` .
259
259
To customize what should be returned by ` require(esm) ` directly, the ES Module can export the
260
260
desired value using the string name ` "module.exports" ` .
@@ -346,7 +346,7 @@ require(X) from module at path Y
346
346
a. return the core module
347
347
b. STOP
348
348
2. If X begins with '/'
349
- a. set Y to be the file system root
349
+ a. set Y to the file system root
350
350
3. If X begins with './' or '/' or '../'
351
351
a. LOAD_AS_FILE(Y + X)
352
352
b. LOAD_AS_DIRECTORY(Y + X)
@@ -357,14 +357,6 @@ require(X) from module at path Y
357
357
6. LOAD_NODE_MODULES(X, dirname(Y))
358
358
7. THROW "not found"
359
359
360
- MAYBE_DETECT_AND_LOAD(X)
361
- 1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
362
- 2. Else, if the source code of X can be parsed as ECMAScript module using
363
- <a href="esm.md#resolver-algorithm-specification">DETECT_MODULE_SYNTAX defined in
364
- the ESM resolver</a>,
365
- a. Load X as an ECMAScript module. STOP.
366
- 3. THROW the SyntaxError from attempting to parse X as CommonJS in 1. STOP.
367
-
368
360
LOAD_AS_FILE(X)
369
361
1. If X is a file, load X as its file extension format. STOP
370
362
2. If X.js is a file,
@@ -373,20 +365,17 @@ LOAD_AS_FILE(X)
373
365
1. MAYBE_DETECT_AND_LOAD(X.js)
374
366
c. If the SCOPE/package.json contains "type" field,
375
367
1. If the "type" field is "module", load X.js as an ECMAScript module. STOP.
376
- 2. If the "type" field is "commonjs", load X.js as an CommonJS module. STOP.
368
+ 2. If the "type" field is "commonjs", load X.js as a CommonJS module. STOP.
377
369
d. MAYBE_DETECT_AND_LOAD(X.js)
378
370
3. If X.json is a file, load X.json to a JavaScript Object. STOP
379
371
4. If X.node is a file, load X.node as binary addon. STOP
380
372
381
- LOAD_INDEX(X)
382
- 1. If X/index.js is a file
383
- a. Find the closest package scope SCOPE to X.
384
- b. If no scope was found, load X/index.js as a CommonJS module. STOP.
385
- c. If the SCOPE/package.json contains "type" field,
386
- 1. If the "type" field is "module", load X/index.js as an ECMAScript module. STOP.
387
- 2. Else, load X/index.js as an CommonJS module. STOP.
388
- 2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
389
- 3. If X/index.node is a file, load X/index.node as binary addon. STOP
373
+ MAYBE_DETECT_AND_LOAD(X)
374
+ 1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
375
+ 2. If the source code of X can be parsed as an ECMAScript module using
376
+ <a href="esm.md#resolver-algorithm-specification">DETECT_MODULE_SYNTAX defined in
377
+ the ESM resolver</a>, Load X as an ECMAScript module. STOP.
378
+ 3. THROW the SyntaxError from attempting to parse X as CommonJS in 1. STOP.
390
379
391
380
LOAD_AS_DIRECTORY(X)
392
381
1. If X/package.json is a file,
@@ -399,6 +388,16 @@ LOAD_AS_DIRECTORY(X)
399
388
g. THROW "not found"
400
389
2. LOAD_INDEX(X)
401
390
391
+ LOAD_INDEX(X)
392
+ 1. If X/index.js is a file
393
+ a. Find the closest package scope SCOPE to X.
394
+ b. If no scope was found, load X/index.js as a CommonJS module. STOP.
395
+ c. If the SCOPE/package.json contains "type" field,
396
+ 1. If the "type" field is "module", load X/index.js as an ECMAScript module. STOP.
397
+ 2. Else, load X/index.js as a CommonJS module. STOP.
398
+ 2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
399
+ 3. If X/index.node is a file, load X/index.node as binary addon. STOP
400
+
402
401
LOAD_NODE_MODULES(X, START)
403
402
1. let DIRS = NODE_MODULES_PATHS(START)
404
403
2. for each DIR in DIRS:
@@ -472,8 +471,8 @@ will not cause the module code to be executed multiple times. This is an
472
471
important feature. With it, "partially done" objects can be returned, thus
473
472
allowing transitive dependencies to be loaded even when they would cause cycles.
474
473
475
- To have a module execute code multiple times, export a function, and call that
476
- function.
474
+ To have a module execute code multiple times, move such code into an exported function
475
+ and call that function.
477
476
478
477
### Module caching caveats
479
478
@@ -488,7 +487,7 @@ Additionally, on case-insensitive file systems or operating systems, different
488
487
resolved filenames can point to the same file, but the cache will still treat
489
488
them as different modules and will reload the file multiple times. For example,
490
489
` require('./foo') ` and ` require('./FOO') ` return two different objects,
491
- irrespective of whether or not ` ./foo ` and ` ./FOO ` are the same file.
490
+ irrespective of whether or not ` ./foo ` and ` ./FOO ` point to the same file.
492
491
493
492
## Built-in modules
494
493
@@ -516,9 +515,11 @@ by that name.
516
515
517
516
Some built-in modules are always preferentially loaded if their identifier is
518
517
passed to ` require() ` . For instance, ` require('http') ` will always
519
- return the built-in HTTP module, even if there is a file by that name. The list
520
- of built-in modules that can be loaded without using the ` node: ` prefix is exposed
521
- in [ ` module.builtinModules ` ] [ ] , listed without the prefix.
518
+ return the built-in HTTP module, even if there is a file by that name.
519
+
520
+ The list of all the built-in modules can be retrieved from [ ` module.builtinModules ` ] [ ] .
521
+ The modules being all listed without the ` node: ` prefix, except those that mandate such
522
+ prefix (as explained in the next section).
522
523
523
524
### Built-in modules with mandatory ` node: ` prefix
524
525
@@ -532,8 +533,6 @@ taken the name. Currently the built-in modules that requires the `node:` prefix
532
533
* [ ` node:test ` ] [ ]
533
534
* [ ` node:test/reporters ` ] [ ]
534
535
535
- The list of these modules is exposed in [ ` module.builtinModules ` ] [ ] , including the prefix.
536
-
537
536
## Cycles
538
537
539
538
<!-- type=misc-->
@@ -543,9 +542,9 @@ executing when it is returned.
543
542
544
543
Consider this situation:
545
544
546
- ` a.js ` :
547
-
548
545
``` js
546
+ // a.js
547
+
549
548
console .log (' a starting' );
550
549
exports .done = false ;
551
550
const b = require (' ./b.js' );
@@ -554,9 +553,9 @@ exports.done = true;
554
553
console .log (' a done' );
555
554
```
556
555
557
- ` b.js ` :
558
-
559
556
``` js
557
+ // b.js
558
+
560
559
console .log (' b starting' );
561
560
exports .done = false ;
562
561
const a = require (' ./a.js' );
@@ -565,20 +564,22 @@ exports.done = true;
565
564
console .log (' b done' );
566
565
```
567
566
568
- ` main.js ` :
569
-
570
567
``` js
568
+ // main.js
569
+
571
570
console .log (' main starting' );
572
571
const a = require (' ./a.js' );
573
572
const b = require (' ./b.js' );
574
573
console .log (' in main, a.done = %j, b.done = %j' , a .done , b .done );
575
574
```
576
575
577
- When ` main.js ` loads ` a.js ` , then ` a.js ` in turn loads ` b.js ` . At that
578
- point, ` b.js ` tries to load ` a.js ` . In order to prevent an infinite
579
- loop, an ** unfinished copy** of the ` a.js ` exports object is returned to the
580
- ` b.js ` module. ` b.js ` then finishes loading, and its ` exports ` object is
581
- provided to the ` a.js ` module.
576
+ Where ` main.js ` loads ` a.js ` , then ` a.js ` in turn loads ` b.js ` , and
577
+ ` b.js ` loads ` a.js ` .
578
+
579
+ In order to prevent an infinite loop, an ** unfinished copy** of the
580
+ ` a.js ` exports object is returned to the ` b.js ` module. ` b.js ` then
581
+ finishes loading, and its ` exports ` object is provided to the ` a.js `
582
+ module.
582
583
583
584
By the time ` main.js ` has loaded both modules, they're both finished.
584
585
The output of this program would thus be:
@@ -618,12 +619,12 @@ A required module prefixed with `'/'` is an absolute path to the file. For
618
619
example, ` require('/home/marco/foo.js') ` will load the file at
619
620
` /home/marco/foo.js ` .
620
621
621
- A required module prefixed with ` './' ` is relative to the file calling
622
+ A required module prefixed with ` './' ` or ` '../' ` is relative to the file calling
622
623
` require() ` . That is, ` circle.js ` must be in the same directory as ` foo.js ` for
623
624
` require('./circle') ` to find it.
624
625
625
626
Without a leading ` '/' ` , ` './' ` , or ` '../' ` to indicate a file, the module must
626
- either be a core module or is loaded from a ` node_modules ` folder.
627
+ either be a core module or being loaded from a ` node_modules ` folder.
627
628
628
629
If the given path does not exist, ` require() ` will throw a
629
630
[ ` MODULE_NOT_FOUND ` ] [ ] error.
@@ -677,13 +678,13 @@ folders as modules, and work for both `require` and `import`.
677
678
678
679
If the module identifier passed to ` require() ` is not a
679
680
[ built-in] ( #built-in-modules ) module, and does not begin with ` '/' ` , ` '../' ` , or
680
- ` './' ` , then Node.js starts at the directory of the current module, and
681
+ ` './' ` , then Node.js starts at the directory of the current module,
681
682
adds ` /node_modules ` , and attempts to load the module from that location.
682
683
Node.js will not append ` node_modules ` to a path already ending in
683
684
` node_modules ` .
684
685
685
- If it is not found there, then it moves to the parent directory, and so
686
- on, until the root of the file system is reached.
686
+ If it is not found there, then Node.js moves to the parent directory and repeats
687
+ the process until either the module is found or the root of the file system is reached.
687
688
688
689
For example, if the file at ` '/home/ry/projects/foo.js' ` called
689
690
` require('bar.js') ` , then Node.js would look in the following locations, in
@@ -741,7 +742,7 @@ folder. These will be loaded faster, and more reliably.
741
742
742
743
<!-- type=misc -->
743
744
744
- Before a module's code is executed, Node.js will wrap it with a function
745
+ Before a CommonJS module's code is executed, Node.js will wrap it with a function
745
746
wrapper that looks like the following:
746
747
747
748
``` js
@@ -805,24 +806,24 @@ See [`__dirname`][] for the directory name of the current module.
805
806
806
807
Examples:
807
808
808
- Running ` node example.js ` from ` /Users/mjr `
809
+ * Running ` node example.js ` from ` /Users/mjr `
809
810
810
- ``` js
811
- console .log (__filename );
812
- // Prints: /Users/mjr/example.js
813
- console .log (__dirname );
814
- // Prints: /Users/mjr
815
- ```
811
+ ``` js
812
+ console .log (__filename );
813
+ // Prints: /Users/mjr/example.js
814
+ console .log (__dirname );
815
+ // Prints: /Users/mjr
816
+ ```
816
817
817
- Given two modules: ` a ` and ` b ` , where ` b ` is a dependency of
818
- ` a ` and there is a directory structure of:
818
+ * Given two modules: ` a ` and ` b ` , where ` b ` is a dependency of
819
+ ` a ` and there is a directory structure of:
819
820
820
- * ` /Users/mjr/app/a.js `
821
- * ` /Users/mjr/app/node_modules/b/b.js `
821
+ * ` /Users/mjr/app/a.js `
822
+ * ` /Users/mjr/app/node_modules/b/b.js `
822
823
823
- References to ` __filename ` within ` b.js ` will return
824
- ` /Users/mjr/app/node_modules/b/b.js ` while references to ` __filename ` within
825
- ` a.js ` will return ` /Users/mjr/app/a.js ` .
824
+ References to ` __filename ` within ` b.js ` will return
825
+ ` /Users/mjr/app/node_modules/b/b.js ` while references to ` __filename ` within
826
+ ` a.js ` will return ` /Users/mjr/app/a.js ` .
826
827
827
828
### ` exports `
828
829
@@ -871,15 +872,17 @@ the current working directory. The relative paths of POSIX style are resolved
871
872
in an OS independent fashion, meaning that the examples above will work on
872
873
Windows in the same way they would on Unix systems.
873
874
875
+ Example:
876
+
874
877
``` js
875
878
// Importing a local module with a path relative to the `__dirname` or current
876
- // working directory. (On Windows, this would resolve to .\path\myLocalModule. )
879
+ // working directory. (On Windows, this would resolve to .\path\myLocalModule)
877
880
const myLocalModule = require (' ./path/myLocalModule' );
878
881
879
882
// Importing a JSON file:
880
883
const jsonData = require (' ./path/filename.json' );
881
884
882
- // Importing a module from node_modules or Node.js built-in module:
885
+ // Importing a module from node_modules or a Node.js built-in module:
883
886
const crypto = require (' node:crypto' );
884
887
```
885
888
@@ -1001,10 +1004,10 @@ changes:
1001
1004
is checked from this location.
1002
1005
* Returns: {string}
1003
1006
1004
- Use the internal ` require() ` machinery to look up the location of a module,
1005
- but rather than loading the module, just return the resolved filename.
1007
+ Uses the internal ` require() ` machinery to look up the location of a module,
1008
+ but rather than loading the module, just returns the resolved filename.
1006
1009
1007
- If the module can not be found, a ` MODULE_NOT_FOUND ` error is thrown.
1010
+ If the module can not be found, a [ ` MODULE_NOT_FOUND ` ] [ ] error is thrown.
1008
1011
1009
1012
##### ` require.resolve.paths(request) `
1010
1013
@@ -1084,19 +1087,19 @@ a.on('ready', () => {
1084
1087
```
1085
1088
1086
1089
Assignment to ` module.exports ` must be done immediately. It cannot be
1087
- done in any callbacks. This does not work:
1088
-
1089
- ` x.js ` :
1090
+ done in any callbacks. For example this does not work:
1090
1091
1091
1092
``` js
1093
+ // x.js
1094
+
1092
1095
setTimeout (() => {
1093
1096
module .exports = { a: ' hello' };
1094
1097
}, 0 );
1095
1098
```
1096
1099
1097
- ` y.js ` :
1098
-
1099
1100
``` js
1101
+ // y.js
1102
+
1100
1103
const x = require (' ./x' );
1101
1104
console .log (x .a );
1102
1105
```
@@ -1209,7 +1212,7 @@ deprecated:
1209
1212
1210
1213
The module that first required this one, or ` null ` if the current module is the
1211
1214
entry point of the current process, or ` undefined ` if the module was loaded by
1212
- something that is not a CommonJS module (E.G .: REPL or ` import ` ).
1215
+ something that is not a CommonJS module (e.g .: REPL or ` import ` ).
1213
1216
1214
1217
### ` module.path `
1215
1218
@@ -1269,9 +1272,9 @@ This section was moved to
1269
1272
1270
1273
* <a id =" modules_module_findsourcemap_path_error " href =" module.html#modulefindsourcemappath " >` module.findSourceMap(path) ` </a >
1271
1274
* <a id =" modules_class_module_sourcemap " href =" module.html#class-modulesourcemap " >Class: ` module.SourceMap ` </a >
1272
- * <a id =" modules_new_sourcemap_payload " href =" module.html#new-sourcemappayload " >` new SourceMap(payload) ` </a >
1273
- * <a id =" modules_sourcemap_payload " href =" module.html#sourcemappayload " >` sourceMap.payload ` </a >
1274
- * <a id =" modules_sourcemap_findentry_linenumber_columnnumber " href =" module.html#sourcemapfindentrylinenumber-columnnumber " >` sourceMap.findEntry(lineNumber, columnNumber) ` </a >
1275
+ * <a id =" modules_new_sourcemap_payload " href =" module.html#new-sourcemappayload " >` new SourceMap(payload) ` </a >
1276
+ * <a id =" modules_sourcemap_payload " href =" module.html#sourcemappayload " >` sourceMap.payload ` </a >
1277
+ * <a id =" modules_sourcemap_findentry_linenumber_columnnumber " href =" module.html#sourcemapfindentrylinenumber-columnnumber " >` sourceMap.findEntry(lineNumber, columnNumber) ` </a >
1275
1278
1276
1279
[ Determining module system ] : packages.md#determining-module-system
1277
1280
[ ECMAScript Modules ] : esm.md
@@ -1299,6 +1302,7 @@ This section was moved to
1299
1302
[ `process.features.require_module` ] : process.md#processfeaturesrequire_module
1300
1303
[ `require.main` ] : #requiremain
1301
1304
[ exports shortcut ] : #exports-shortcut
1305
+ [ module namespace object ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import#module_namespace_object
1302
1306
[ module resolution ] : #all-together
1303
1307
[ native addons ] : addons.md
1304
1308
[ subpath exports ] : packages.md#subpath-exports
0 commit comments