Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
161 commits
Select commit Hold shift + click to select a range
4dd1f42
src: gracefully handle errors in GetX509NameObject
tniessen Jan 11, 2022
f1ac552
deps: V8: cherry-pick 3b6b21f595f6
targos Jan 14, 2022
e54f52f
test: do not OR F_OK in fs.access() test
cjihrig Jan 11, 2022
91b9052
doc: expand fs.access() mode parameter docs
cjihrig Jan 12, 2022
cd075f4
test: remove broken wiki link from test/common doc
kuriyosh Jan 14, 2022
c22177f
src: use `std::optional` for Worker thread id
addaleax Jan 14, 2022
18b8334
doc: add missing YAML tag in `esm.md`
aduh95 Jan 14, 2022
0f31d29
test: improve test coverage of dns/promises
kuriyosh Jan 14, 2022
08fc4b5
doc: add missing word in readable.read() text
Trott Jan 14, 2022
aaa4306
deps: upgrade npm to 8.3.1
npm-robot Jan 14, 2022
2ea2621
build: fix node build failures in WSL Ubuntu
MrJithil Jan 11, 2022
f7be6ab
stream: remove always-false condition check
Trott Jan 15, 2022
807c7e1
tls: move tls.parseCertString to end-of-life
tniessen Jan 11, 2022
e874630
doc: add Mesteery to collaborators
Mesteery Jan 15, 2022
f92af52
doc: fix typo in `onboarding.md`
aduh95 Jan 15, 2022
b2edcfe
doc: remove statement about client private keys
tniessen Jan 13, 2022
67beba8
meta: update AUTHORS
nodejs-github-bot Jan 16, 2022
986cf3b
doc: remove statement about (EC)DHE performance
tniessen Jan 14, 2022
a7215c8
benchmark: remove unreachable code from crypto/hash-stream-creation
Trott Jan 17, 2022
20347d5
stream: avoid function call where possible
Trott Jan 17, 2022
0594577
test: add coverage for util.inspect()
Trott Jan 14, 2022
8732591
doc: clarify module system selection
aduh95 Jan 17, 2022
3f0bcfb
stream: add forEach method
benjamingr Jan 8, 2022
5f4f8d4
tools: update doc to highlight.js@11.4.0 to-vfile@7.2.3
nodejs-github-bot Jan 17, 2022
d0d8320
doc: improve Web Crypto headings related to ECC
tniessen Jan 15, 2022
8cba65f
doc: adjust assignment in condition in stream doc
Trott Jan 17, 2022
f458f1b
doc: fix typos in esm.md
yu521088 Jan 17, 2022
8de858b
test: increase coverage for stream writable
Trott Jan 17, 2022
6b9d2ae
test: add missing await in fs-rm/fs-rmdir tests
Jan 15, 2022
a1f827a
policy: replace entries with keys
VoltrexKeyva Jan 17, 2022
6beee31
tools: bump eslint from 8.6.0 to 8.7.0
Trott Jan 18, 2022
c4f5a49
doc: do not reference SSL when discussing SNI
tniessen Jan 16, 2022
331b906
build: fix npm version detection
targos Jan 18, 2022
eceb2e7
doc: make Web Crypto example spec compliant
tniessen Jan 16, 2022
cc97ea5
tools: update lint-md-dependencies
nodejs-github-bot Jan 8, 2022
325b947
2022-01-18, Version 17.4.0 (Current)
targos Jan 16, 2022
98659c0
doc: use sentence case for Web Crypto headers
tniessen Jan 18, 2022
8ed577a
doc,readline: add missing node protocol in example
Mesteery Jan 18, 2022
50b7ab9
build: fix libuv builds for android aarch64
RaisinTen Jan 18, 2022
f8f3d35
tools: add missing `.PHONY` and `.NOTPARALLEL` targets in `Makefile`
aduh95 Jan 18, 2022
7a07df4
doc: edit async_context context loss text
Trott Jan 18, 2022
56679eb
doc: recommend package exports instead of requiring folders
aduh95 Jan 18, 2022
65910c0
tls: represent registeredID numerically always
tniessen Jan 16, 2022
a199387
doc: make contributing info more discoverable
mhdawson Jan 5, 2022
81e88f2
build: add loong64 configure
shipujin Jan 18, 2022
5178332
process: ignore asyncId 0 in exception handler
apapirovski Jan 7, 2022
b9258e5
test: fix typo in test_js_native_api_v8
tniessen Jan 18, 2022
dbc6e39
esm: improve validation of resolved URLs
JakobJingleheimer Jan 19, 2022
2639857
build: add --v8-enable-hugepage flag
Jan 12, 2022
22792c8
domain: pass opts to `EventEmitter.init`
MoonBall Jan 19, 2022
119519e
doc: remove redunant `await` calls from stream docs
gioragutt Jan 19, 2022
da1b59f
crypto: support RFC 2818 compatible checkHost
tniessen Jan 17, 2022
d8f6b38
doc: fix cjs example code for process.arch
Manduro Jan 19, 2022
eda54ba
doc: move Mesteery to collaborators
tniessen Jan 19, 2022
6d66649
crypto: remove checkIP options argument
tniessen Jan 17, 2022
25381da
test: improve util-format code coverage
Trott Jan 19, 2022
8e413ff
doc: simplify util.TextDecoder example
Trott Jan 20, 2022
9759695
lib: modify `DOMException` to pass WPT
XadillaX Jan 14, 2022
5753eb1
dgram: remove unreachable connectState assign
pd4d10 Jan 20, 2022
c4a6f9a
stream: rename unknown primordial
VoltrexKeyva Oct 27, 2021
de9dc41
test: add DataView test entry for whatwg
VoltrexKeyva Oct 27, 2021
5a407d6
stream: add toArray
benjamingr Jan 16, 2022
270253c
deps: update V8 to 9.7.106.18
targos Jan 18, 2022
a189dee
build: reset embedder string to "-node.0"
targos Jan 18, 2022
6ec1664
src: update NODE_MODULE_VERSION to 104
targos Nov 21, 2021
a2802a8
deps: V8: un-cherry-pick bd019bd
refack Mar 27, 2019
0a9c63e
deps: V8: patch register-arm64.h
refack May 22, 2019
c465ba5
deps: V8: forward declaration of `Rtl*FunctionTable`
refack May 22, 2019
efff1bd
deps: make v8.h compatible with VS2015
joaocgreis Sep 22, 2021
c40a7c1
deps: fix V8 build issue with inline methods
gengjiawen Oct 14, 2020
9fef7a1
deps: silence irrelevant V8 warning
targos Jan 30, 2021
b9009d6
deps: silence irrelevant V8 warning
targos May 1, 2021
2080449
deps: disable trap handler for Windows cross-compiler
targos Oct 20, 2021
5ea28b7
deps: V8: cherry-pick 7ae0b77628f6
rayw000 Nov 19, 2021
91cf835
deps: V8: cherry-pick 3b6b21f595f6
targos Jan 14, 2022
696ce7d
deps: V8: cherry-pick 1cc12b278e22
targos Jan 18, 2022
e23e345
deps: V8: cherry-pick 80bbbb143c24
targos Jan 18, 2022
1ad4409
tools: update V8 gypfiles for 9.7
targos Nov 5, 2021
9318408
deps: silence V8's warning on CompileFunction
targos Nov 22, 2021
12608d3
doc: update timingSafeEqual error case
AlexA-1976 Jan 13, 2022
110aa38
doc: fix backticks around 'default'
tniessen Jan 20, 2022
716aefd
doc: fix deprecated alias description in cluster
tniessen Jan 20, 2022
10b0432
test: improve test coverage of internal/worker/io
kuriyosh Jan 20, 2022
ef35175
doc: fix async_hooks example in api docs
marsonya Jan 21, 2022
5badf46
stream: support some and every
benjamingr Jan 17, 2022
271725a
readline: undo previous edit when get key code 0x1F
rayw000 Jan 21, 2022
74867f7
doc: add missing word in cluster.workers details
tniessen Jan 21, 2022
870044f
tools: increase maximum line length to 120 characters
Trott Jan 21, 2022
0879394
doc: demonstrate dangers of `buffer.slice()`
shalvah Jan 21, 2022
ec1364b
build: enable zoslib installation on z/OS
alexcfyung Jan 12, 2022
290911b
lib: remove erroneous JSDoc entry
Trott Jan 19, 2022
830cbca
doc: deprecate `buffer.slice`
benjamingr Jan 19, 2022
6bf769e
benchmark: add `subarray` to `buffer-slice`
benjamingr Jan 19, 2022
3d23d62
buffer: alias `subarray` and `slice`
benjamingr Jan 19, 2022
d0ea81c
test: fix typo in test-stream-toArray
tniessen Jan 21, 2022
8275683
process: use validateString validator
VoltrexKeyva Jan 21, 2022
806c7c1
doc: add note for handling signal events in trace events
thedull Jan 21, 2022
feafea1
lib: fix consistency of methods that emit warnings
kuriyosh Jan 21, 2022
dbe60a2
doc: improve `'hex'` Buffer decoding description and examples
gioragutt Jan 21, 2022
2f17004
readline: add feature yank and yank pop
rayw000 Jan 22, 2022
b8de7aa
crypto: adjust types for getRandomValues
LiviaMedeiros Jan 11, 2022
5aa4010
crypto: remove wildcard options for checkEmail
tniessen Jan 19, 2022
18365d8
crypto: change default check(Host|Email) behavior
tniessen Jan 19, 2022
a96549a
doc: add 16 and 17 to previous versions
aduh95 Jan 22, 2022
d01c645
test: move test-gc-http-client-onerror to sequential
lpinca Jan 22, 2022
312b0fc
doc: suggest worker threads in cluster docs
tniessen Jan 23, 2022
b09d1f4
tools: update lint-md-dependencies to rollup@2.65.0
nodejs-github-bot Jan 23, 2022
fbfb61a
doc: simplify readline/stdin text
Trott Jan 23, 2022
c05c883
crypto: revise variables for const use instead of let
Trott Jan 20, 2022
e6fdcbe
stream: check for null instead of falsy in loops
Trott Jan 20, 2022
0bc6422
policy: check for null instead of falsy in loop
Trott Jan 20, 2022
e044a5d
process: check for null instead of falsy in while loop
Trott Jan 20, 2022
bd42245
timers: check for nullish instead of falsy in loops
Trott Jan 20, 2022
ac06951
util: check for null instead of flasy in loop
Trott Jan 20, 2022
348469b
repl: check for precise values rather than falsy in loops
Trott Jan 20, 2022
d77754b
test: prepare tests for no-cond-assign ESLint rule
Trott Jan 20, 2022
68bb83f
tools: enable no-cond-assign-ESLint rule
Trott Jan 20, 2022
cc8931a
stream: support flatMap
benjamingr Jan 20, 2022
ca48949
stream: never flatten on toArray
benjamingr Jan 23, 2022
c18ca14
tools: fix typo in `tools/code_cache/README.md`
tniessen Jan 23, 2022
0e6db9c
doc: modernize and simplify cluster example
tniessen Jan 23, 2022
3d27a04
meta: update AUTHORS
nodejs-github-bot Jan 23, 2022
a8afe26
stream: add drop and take
benjamingr Jan 21, 2022
7ce8403
test: simplify test-gc-http-client
lpinca Jan 20, 2022
400b7c2
tools: use Set instead of { [key]: true } object
tniessen Jan 24, 2022
c0ef0d5
deps: upgrade npm to 8.3.2
npm-robot Jan 20, 2022
49ba210
util: remove unused fast path in internal debuglog
Trott Jan 24, 2022
3ee8c3e
tools: add compile_commands to ignore file
yashLadha Jan 24, 2022
ce41395
test: add stream map tests
benjamingr Jan 22, 2022
3657c14
doc: improve TLS/SSL introduction
tniessen Jan 24, 2022
726711f
node-api: add node_api_symbol_for()
RaisinTen Dec 26, 2021
5d88f59
doc: fix documentation for `MODULE_NOT_FOUND` and `ERR_MODULE_NOT_FOUND`
aduh95 Jan 24, 2022
b27ae24
lib: throw error in structuedClone when no arguments are passed
gioragutt Jan 24, 2022
938ab0e
test: remove error allowance in debugger test
MrJithil Jan 25, 2022
88a3bd5
doc: clarify treatment of non-string argument to new URL()
Trott Jan 25, 2022
6023bed
doc: revise url.resolve() text
Trott Jan 25, 2022
ff5766f
test: remove unneeded test statement
Trott Jan 25, 2022
e2e2bc8
test: use Object.hasOwn() where applicable
Trott Jan 25, 2022
a2dead4
doc: clarify parameter for napi_get_cb_info
mhdawson Jan 21, 2022
670dbba
doc: document flow for supporting type generation
mhdawson Jan 10, 2022
6fc6ba7
doc: remove unadvisable cluster example
tniessen Jan 25, 2022
ccb8aae
doc: fix typo in `technical-priorities.md`
marsonya Jan 26, 2022
c15639d
doc: clarify treatment of non-string base in URL()
Trott Jan 27, 2022
830007d
stream: add asIndexedPairs
benjamingr Jan 24, 2022
7752eed
test: improve stability of oom test
benjamingr Jan 26, 2022
5d9caa1
stream: use synchronous error validation on iteration helpers
iMoses Jan 27, 2022
acc92a7
util: use hasOwnProperty() primordial
Trott Jan 25, 2022
40ef144
doc: fix link to npm documentation
aduh95 Jan 27, 2022
a4183b6
doc: improve docs to give descriptive info for the platform property
twitharshil Jan 28, 2022
5a61ca6
doc: fix typo in contributing guides
kuriyosh Jan 28, 2022
f98a785
http2: fix memory leak on nghttp2 hd threshold
RafaelGSS Jan 28, 2022
334fb38
tools: use Set instead of { [key]: true } object
tniessen Jan 28, 2022
0172d1d
doc: avoid incomplete sentence in cluster docs
tniessen Jan 28, 2022
64c4b55
process: unhandledRejection support more errors
benjamingr Jan 28, 2022
d86dcaa
perf_hooks: remove useless calls in Histogram
mhdawson Jan 18, 2022
2818fa6
build: check if python is a executable program
himself65 Dec 30, 2020
44f8b4f
doc: update Mesteery email
Mesteery Jan 29, 2022
ef4c115
test: replace commented out expectations with tests
RaisinTen Jan 29, 2022
74de555
test: fix typo in MessageChannel test
tniessen Jan 29, 2022
3e0526c
meta: adjust mailmap/AUTHORS to reflect README change
Trott Jan 29, 2022
2ded293
build: improve consistency between workflows
Mesteery Nov 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doc: clarify module system selection
Refs: #41345 (comment)

PR-URL: #41383
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
aduh95 authored Jan 17, 2022
commit 87325917ec2e57c8b70eba78695dbeaa1ac181a5
44 changes: 38 additions & 6 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Command-line options
# Command-line API

<!--introduced_in=v5.9.1-->

Expand All @@ -11,16 +11,43 @@ To view this documentation as a manual page in a terminal, run `man node`.

## Synopsis

`node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]`
`node [options] [V8 options] [<program-entry-point> | -e "script" | -] [--] [arguments]`

`node inspect [script.js | -e "script" | <host>:<port>] …`
`node inspect [<program-entry-point> | -e "script" | <host>:<port>] …`

`node --v8-options`

Execute without arguments to start the [REPL][].

For more info about `node inspect`, see the [debugger][] documentation.

## Program entry point

The program entry point is a specifier-like string. If the string is not an
absolute path, it's resolved as a relative path from the current working
directory. That path is then resolved by [CommonJS][] module loader. If no
corresponding file is found, an error is thrown.

If a file is found, its path will be passed to the [ECMAScript module loader][]
under any of the following conditions:

* The program was started with a command-line flag that forces the entry
point to be loaded with ECMAScript module loader.
* The file has an `.mjs` extension.
* The file does not have a `.cjs` extension, and the nearest parent
`package.json` file contains a top-level [`"type"`][] field with a value of
`"module"`.

Otherwise, the file is loaded using the CommonJS module loader. See
[Modules loaders][] for more details.

### ECMAScript modules loader entry point caveat

When loading [ECMAScript module loader][] loads the program entry point, the `node`
command will only accept as input only files with `.js`, `.mjs`, or `.cjs`
extensions; and with `.wasm` extensions when
[`--experimental-wasm-modules`][] is enabled.

## Options

<!-- YAML
Expand Down Expand Up @@ -277,8 +304,8 @@ Enable experimental JSON support for the ES Module loader.
added: v9.0.0
-->

Specify the `module` of a custom experimental [ECMAScript Module loader][].
`module` may be either a path to a file, or an ECMAScript Module name.
Specify the `module` of a custom experimental [ECMAScript module loader][].
`module` may be any string accepted as an [`import` specifier][].

### `--experimental-policy`

Expand Down Expand Up @@ -1931,15 +1958,19 @@ $ node --max-old-space-size=1536 index.js
```

[Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/
[ECMAScript Module loader]: esm.md#loaders
[CommonJS]: modules.md
[ECMAScript module loader]: esm.md#loaders
[Modules loaders]: packages.md#modules-loaders
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
[REPL]: repl.md
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage
[Source Map]: https://sourcemaps.info/spec.html
[Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
[V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html
[`"type"`]: packages.md#type
[`--cpu-prof-dir`]: #--cpu-prof-dir
[`--diagnostic-dir`]: #--diagnostic-dirdirectory
[`--experimental-wasm-modules`]: #--experimental-wasm-modules
[`--heap-prof-dir`]: #--heap-prof-dir
[`--openssl-config`]: #--openssl-configfile
[`--redirect-warnings`]: #--redirect-warningsfile
Expand All @@ -1952,6 +1983,7 @@ $ node --max-old-space-size=1536 index.js
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
[`dns.setDefaultResultOrder()`]: dns.md#dnssetdefaultresultorderorder
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
[`import` specifier]: esm.md#import-specifiers
[`process.setUncaughtExceptionCaptureCallback()`]: process.md#processsetuncaughtexceptioncapturecallbackfn
[`tls.DEFAULT_MAX_VERSION`]: tls.md#tlsdefault_max_version
[`tls.DEFAULT_MIN_VERSION`]: tls.md#tlsdefault_min_version
Expand Down
12 changes: 7 additions & 5 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ provides interoperability between them and its original module format,

<!-- type=misc -->

Node.js treats JavaScript code as CommonJS modules by default.
Authors can tell Node.js to treat JavaScript code as ECMAScript modules
Node.js has two module systems: [CommonJS][] modules and ECMAScript modules.

Authors can tell Node.js to use the ECMAScript modules loader
via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the
`--input-type` flag. See
[Modules: Packages](packages.md#determining-module-system) for more
details.
[`--input-type`][] flag. Outside of those cases, Node.js will use the CommonJS
module loader. See [Determining module system][] for more details.

<!-- Anchors to make sure old links find a target -->

Expand Down Expand Up @@ -1443,6 +1443,7 @@ success!
[CommonJS]: modules.md
[Conditional exports]: packages.md#conditional-exports
[Core modules]: modules.md#core-modules
[Determining module system]: packages.md#determining-module-system
[Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
[ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration
[Import Assertions]: #import-assertions
Expand All @@ -1454,6 +1455,7 @@ success!
[WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script
[`"exports"`]: packages.md#exports
[`"type"`]: packages.md#type
[`--input-type`]: cli.md#--input-typetype
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
Expand Down
35 changes: 34 additions & 1 deletion doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,38 @@ module.exports = class Square {
};
```

The module system is implemented in the `require('module')` module.
The CommonJS module system is implemented in the [`module` core module][].

## Enabling

<!-- type=misc -->

Node.js has two module systems: CommonJS modules and [ECMAScript modules][].

By default, Node.js will treat the following as CommonJS modules:

* Files with a `.cjs` extension;

* Files with a `.js` extension when the nearest parent `package.json` file
contains a top-level field [`"type"`][] with a value of `"commonjs"`.

* Files with a `.js` extension when the nearest parent `package.json` file
doesn't contain a top-level field [`"type"`][]. Package authors should include
the [`"type"`][] field, even in packages where all sources are CommonJS. Being
explicit about the `type` of the package will make things easier for build
tools and loaders to determine how the files in the package should be
interpreted.

* Files with an extension that is not `.mjs`, `.cjs`, `.json`, `.node`, or `.js`
(when the nearest parent `package.json` file contains a top-level field
[`"type"`][] with a value of `"module"`, those files will be recognized as
CommonJS modules only if they are being `require`d, not when used as the
command-line entry point of the program).

See [Determining module system][] for more details.

Calling `require()` always use the CommonJS module loader. Calling `import()`
always use the ECMAScript module loader.

## Accessing the main module

Expand Down Expand Up @@ -1047,13 +1078,15 @@ This section was moved to
[ECMAScript Modules]: esm.md
[GLOBAL_FOLDERS]: #loading-from-the-global-folders
[`"main"`]: packages.md#main
[`"type"`]: packages.md#type
[`ERR_REQUIRE_ESM`]: errors.md#err_require_esm
[`Error`]: errors.md#class-error
[`__dirname`]: #__dirname
[`__filename`]: #__filename
[`import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
[`module.children`]: #modulechildren
[`module.id`]: #moduleid
[`module` core module]: module.md
[`module` object]: #the-module-object
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
[`path.dirname()`]: path.md#pathdirnamepath
Expand Down
63 changes: 55 additions & 8 deletions doc/api/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ along with a reference for the [`package.json`][] fields defined by Node.js.
## Determining module system

Node.js will treat the following as [ES modules][] when passed to `node` as the
initial input, or when referenced by `import` statements within ES module code:
initial input, or when referenced by `import` statements or `import()`
expressions:

* Files ending in `.mjs`.
* Files with an `.mjs` extension.

* Files ending in `.js` when the nearest parent `package.json` file contains a
top-level [`"type"`][] field with a value of `"module"`.
* Files with a `.js` extension when the nearest parent `package.json` file
contains a top-level [`"type"`][] field with a value of `"module"`.

* Strings passed in as an argument to `--eval`, or piped to `node` via `STDIN`,
with the flag `--input-type=module`.
Expand All @@ -67,12 +68,13 @@ field, or string input without the flag `--input-type`. This behavior is to
preserve backward compatibility. However, now that Node.js supports both
CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
will treat the following as CommonJS when passed to `node` as the initial input,
or when referenced by `import` statements within ES module code:
or when referenced by `import` statements, `import()` expressions, or
`require()` expressions:

* Files ending in `.cjs`.
* Files with a `.cjs` extension.

* Files ending in `.js` when the nearest parent `package.json` file contains a
top-level field [`"type"`][] with a value of `"commonjs"`.
* Files with a `.js` extension when the nearest parent `package.json` file
contains a top-level field [`"type"`][] with a value of `"commonjs"`.

* Strings passed in as an argument to `--eval` or `--print`, or piped to `node`
via `STDIN`, with the flag `--input-type=commonjs`.
Expand All @@ -83,6 +85,48 @@ future-proof the package in case the default type of Node.js ever changes, and
it will also make things easier for build tools and loaders to determine how the
files in the package should be interpreted.

### Modules loaders

Node.js has two systems for resolving a specifier and loading modules.

There is the CommonJS module loader:

* It is fully synchronous.
* It is responsible for handling `require()` calls.
* It is monkey patchable.
* It supports [folders as modules][].
* When resolving a specifier, if no exact match is found, it will try to add
extensions (`.js`, `.json`, and finally `.node`) and then attempt to resolve
[folders as modules][].
* It treats `.json` as JSON text files.
* `.node` files are interpreted as compiled addon modules loaded with
`process.dlopen()`.
* It treats all files that lack `.json` or `.node` extensions as JavaScript
text files.
* It cannot be used to load ECMAScript modules (although it is possible to
[load ECMASCript modules from CommonJS modules][]). When used to load a
JavaScript text file that is not an ECMAScript module, it loads it as a
CommonJS module.

There is the ECMAScript module loader:

* It is asynchronous.
* It is responsible for handling `import` statements and `import()` expressions.
* It is not monkey patchable, can be customized using [loader hooks][].
* It does not support folders as modules, directory indexes (e.g.
`'./startup/index.js'`) must be fully specified.
* It does no extension searching. A file extension must be provided
when the specifier is a relative or absolute file URL.
* It can load JSON modules, but an import assertion is required (behind
`--experimental-json-modules` flag).
* It accepts only `.js`, `.mjs`, and `.cjs` extensions for JavaScript text
files.
* It can be used to load JavaScript CommonJS modules. Such modules
are passed through the `es-module-lexer` to try to identify named exports,
which are available if they can be determined through static analysis.
Imported CommonJS modules have their URLs converted to absolute
paths and are then loaded via the CommonJS module loader.

### `package.json` and file extensions

Within a package, the [`package.json`][] [`"type"`][] field defines how
Expand Down Expand Up @@ -1236,6 +1280,9 @@ This field defines [subpath imports][] for the current package.
[`esm`]: https://github.com/standard-things/esm#readme
[`package.json`]: #nodejs-packagejson-field-definitions
[entry points]: #package-entry-points
[folders as modules]: modules.md#folders-as-modules
[load ECMASCript modules from CommonJS modules]: modules.md#the-mjs-extension
[loader hooks]: esm.md#loaders
[self-reference]: #self-referencing-a-package-using-its-name
[subpath exports]: #subpath-exports
[subpath imports]: #subpath-imports
Expand Down
2 changes: 1 addition & 1 deletion doc/api/synopsis.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ Now, open any preferred web browser and visit `http://127.0.0.1:3000`.
If the browser displays the string `Hello, World!`, that indicates
the server is working.

[Command-line options]: cli.md#command-line-options
[Command-line options]: cli.md#options
[Installing Node.js via package manager]: https://nodejs.org/en/download/package-manager/
[web server]: http.md
4 changes: 2 additions & 2 deletions tools/doc/links-mapper.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"doc/api/synopsis.md": {
"command line options": "cli.html#command-line-options",
"command line options": "cli.html#options",
"web server": "http.html"
}
}
}