Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6460b1b

Browse filesBrowse files
aduh95targos
authored andcommitted
doc: clarify require behavior with non .js extensions
Refs: https://github.com/nodejs/node/discussions/41333 PR-URL: #41345 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 2e92f6f commit 6460b1b
Copy full SHA for 6460b1b

File tree

Expand file treeCollapse file tree

1 file changed

+18
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-9
lines changed
Open diff view settings
Collapse file

‎doc/api/modules.md‎

Copy file name to clipboardExpand all lines: doc/api/modules.md
+18-9Lines changed: 18 additions & 9 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -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

@@ -375,11 +378,15 @@ correctly within an application.
375378

376379
If the exact filename is not found, then Node.js will attempt to load the
377380
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')`).
379384

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.
383390

384391
A required module prefixed with `'/'` is an absolute path to the file. For
385392
example, `require('/home/marco/foo.js')` will load the file at
@@ -1036,19 +1043,21 @@ This section was moved to
10361043
* <a id="modules_sourcemap_payload" href="module.html#sourcemappayload">`sourceMap.payload`</a>
10371044
* <a id="modules_sourcemap_findentry_linenumber_columnnumber" href="module.html#sourcemapfindentrylinenumber-columnnumber">`sourceMap.findEntry(lineNumber, columnNumber)`</a>
10381045

1046+
[Determining module system]: packages.md#determining-module-system
10391047
[ECMAScript Modules]: esm.md
10401048
[GLOBAL_FOLDERS]: #loading-from-the-global-folders
10411049
[`"main"`]: packages.md#main
1050+
[`ERR_REQUIRE_ESM`]: errors.md#err_require_esm
10421051
[`Error`]: errors.md#class-error
10431052
[`__dirname`]: #__dirname
10441053
[`__filename`]: #__filename
1054+
[`import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
10451055
[`module.children`]: #modulechildren
10461056
[`module.id`]: #moduleid
10471057
[`module` object]: #the-module-object
10481058
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
10491059
[`path.dirname()`]: path.md#pathdirnamepath
10501060
[`require.main`]: #requiremain
1051-
[an error]: errors.md#err_require_esm
10521061
[exports shortcut]: #exports-shortcut
10531062
[module resolution]: #all-together
10541063
[native addons]: addons.md

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.