@@ -133,10 +133,13 @@ relative, and based on the real path of the files making the calls to
133
133
134
134
## The ` .mjs ` extension
135
135
136
- It is not possible to ` require() ` files that have the ` .mjs ` extension.
137
- Attempting to do so will throw [ an error] [ ] . The ` .mjs ` extension is
138
- reserved for [ ECMAScript Modules] [ ] which cannot be loaded via ` require() ` .
139
- See [ ECMAScript Modules] [ ] for more details.
136
+ Due to the synchronous nature of ` require() ` , it is not possible to use it to
137
+ load ECMAScript module files. Attempting to do so will throw a
138
+ [ ` ERR_REQUIRE_ESM ` ] [ ] error. Use [ ` import() ` ] [ ] instead.
139
+
140
+ The ` .mjs ` extension is reserved for [ ECMAScript Modules] [ ] which cannot be
141
+ loaded via ` require() ` . See [ Determining module system] [ ] section for more info
142
+ regarding which files are parsed as ECMAScript modules.
140
143
141
144
## All together...
142
145
@@ -375,11 +378,15 @@ correctly within an application.
375
378
376
379
If the exact filename is not found, then Node.js will attempt to load the
377
380
required filename with the added extensions: ` .js ` , ` .json ` , and finally
378
- ` .node ` .
381
+ ` .node ` . When loading a file that has a different extension (e.g. ` .cjs ` ), its
382
+ full name must be passed to ` require() ` , including its file extension (e.g.
383
+ ` require('./file.cjs') ` ).
379
384
380
- ` .js ` files are interpreted as JavaScript text files, and ` .json ` files are
381
- parsed as JSON text files. ` .node ` files are interpreted as compiled addon
382
- modules loaded with ` process.dlopen() ` .
385
+ ` .json ` files are parsed as JSON text files, ` .node ` files are interpreted as
386
+ compiled addon modules loaded with ` process.dlopen() ` . Files using any other
387
+ extension (or no extension at all) are parsed as JavaScript text files. Refer to
388
+ the [ Determining module system] [ ] section to understand what parse goal will be
389
+ used.
383
390
384
391
A required module prefixed with ` '/' ` is an absolute path to the file. For
385
392
example, ` require('/home/marco/foo.js') ` will load the file at
@@ -1036,19 +1043,21 @@ This section was moved to
1036
1043
* <a id =" modules_sourcemap_payload " href =" module.html#sourcemappayload " >` sourceMap.payload ` </a >
1037
1044
* <a id =" modules_sourcemap_findentry_linenumber_columnnumber " href =" module.html#sourcemapfindentrylinenumber-columnnumber " >` sourceMap.findEntry(lineNumber, columnNumber) ` </a >
1038
1045
1046
+ [ Determining module system ] : packages.md#determining-module-system
1039
1047
[ ECMAScript Modules ] : esm.md
1040
1048
[ GLOBAL_FOLDERS ] : #loading-from-the-global-folders
1041
1049
[ `"main"` ] : packages.md#main
1050
+ [ `ERR_REQUIRE_ESM` ] : errors.md#err_require_esm
1042
1051
[ `Error` ] : errors.md#class-error
1043
1052
[ `__dirname` ] : #__dirname
1044
1053
[ `__filename` ] : #__filename
1054
+ [ `import()` ] : https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
1045
1055
[ `module.children` ] : #modulechildren
1046
1056
[ `module.id` ] : #moduleid
1047
1057
[ `module` object ] : #the-module-object
1048
1058
[ `package.json` ] : packages.md#nodejs-packagejson-field-definitions
1049
1059
[ `path.dirname()` ] : path.md#pathdirnamepath
1050
1060
[ `require.main` ] : #requiremain
1051
- [ an error ] : errors.md#err_require_esm
1052
1061
[ exports shortcut ] : #exports-shortcut
1053
1062
[ module resolution ] : #all-together
1054
1063
[ native addons ] : addons.md
0 commit comments