@@ -133,10 +133,13 @@ relative, and based on the real path of the files making the calls to
133133
134134## The ` .mjs ` extension
135135
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.
140143
141144## All together...
142145
@@ -373,11 +376,15 @@ correctly within an application.
373376
374377If the exact filename is not found, then Node.js will attempt to load the
375378required filename with the added extensions: ` .js ` , ` .json ` , and finally
376- ` .node ` .
379+ ` .node ` . When loading a file that has a different extension (e.g. ` .cjs ` ), its
380+ full name must be passed to ` require() ` , including its file extension (e.g.
381+ ` require('./file.cjs') ` ).
377382
378- ` .js ` files are interpreted as JavaScript text files, and ` .json ` files are
379- parsed as JSON text files. ` .node ` files are interpreted as compiled addon
380- modules loaded with ` process.dlopen() ` .
383+ ` .json ` files are parsed as JSON text files, ` .node ` files are interpreted as
384+ compiled addon modules loaded with ` process.dlopen() ` . Files using any other
385+ extension (or no extension at all) are parsed as JavaScript text files. Refer to
386+ the [ Determining module system] [ ] section to understand what parse goal will be
387+ used.
381388
382389A required module prefixed with ` '/' ` is an absolute path to the file. For
383390example, ` require('/home/marco/foo.js') ` will load the file at
@@ -1032,19 +1039,21 @@ This section was moved to
10321039 * <a id =" modules_sourcemap_payload " href =" module.html#sourcemappayload " >` sourceMap.payload ` </a >
10331040 * <a id =" modules_sourcemap_findentry_linenumber_columnnumber " href =" module.html#sourcemapfindentrylinenumber-columnnumber " >` sourceMap.findEntry(lineNumber, columnNumber) ` </a >
10341041
1042+ [ Determining module system ] : packages.md#determining-module-system
10351043[ ECMAScript Modules ] : esm.md
10361044[ GLOBAL_FOLDERS ] : #loading-from-the-global-folders
10371045[ `"main"` ] : packages.md#main
1046+ [ `ERR_REQUIRE_ESM` ] : errors.md#err_require_esm
10381047[ `Error` ] : errors.md#class-error
10391048[ `__dirname` ] : #__dirname
10401049[ `__filename` ] : #__filename
1050+ [ `import()` ] : https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
10411051[ `module.children` ] : #modulechildren
10421052[ `module.id` ] : #moduleid
10431053[ `module` object ] : #the-module-object
10441054[ `package.json` ] : packages.md#nodejs-packagejson-field-definitions
10451055[ `path.dirname()` ] : path.md#pathdirnamepath
10461056[ `require.main` ] : #requiremain
1047- [ an error ] : errors.md#err_require_esm
10481057[ exports shortcut ] : #exports-shortcut
10491058[ module resolution ] : #all-together
10501059[ native addons ] : addons.md
0 commit comments