Skip to content

Commit 71aa8ee

Browse files
authored
Update docs for 5.8 (#3345)
1 parent 9aabf98 commit 71aa8ee

File tree

17 files changed

+735
-310
lines changed

17 files changed

+735
-310
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@types/eslint": "7.29.0",
3939
"assert": "2.0.0",
4040
"rollup-plugin-typescript2": "0.34.1",
41-
"typescript": "5.7.3",
41+
"typescript": "5.8.1-rc",
4242
"tslib": "^2.6.2",
4343
"prettier": "^2.0.2",
4444
"sharp": "0.28.1"

packages/documentation/copy/en/modules-reference/Reference.md

+23-15
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,21 @@ declare module "*.html" {
199199

200200
## The `module` compiler option
201201

202-
This section discusses the details of each `module` compiler option value. See the [_Module output format_](/docs/handbook/modules/theory.html#the-module-output-format) theory section for more background on what the option is and how it fits into the overall compilation process. In brief, the `module` compiler option was historically only used to control the output module format of emitted JavaScript files. The more recent `node16` and `nodenext` values, however, describe a wide range of characteristics of Node.js’s module system, including what module formats are supported, how the module format of each file is determined, and how different module formats interoperate.
202+
This section discusses the details of each `module` compiler option value. See the [_Module output format_](/docs/handbook/modules/theory.html#the-module-output-format) theory section for more background on what the option is and how it fits into the overall compilation process. In brief, the `module` compiler option was historically only used to control the output module format of emitted JavaScript files. The more recent `node16`, `node18`, and `nodenext` values, however, describe a wide range of characteristics of Node.js’s module system, including what module formats are supported, how the module format of each file is determined, and how different module formats interoperate.
203203

204-
### `node16`, `nodenext`
204+
### `node16`, `node18`, `nodenext`
205+
206+
Node.js supports both CommonJS and ECMAScript modules, with specific rules for which format each file can be and how the two formats are allowed to interoperate. `node16`, `node18`, and `nodenext` describe the full range of behavior for Node.js’s dual-format module system, and **emit files in either CommonJS or ESM format**. This is different from every other `module` option, which are runtime-agnostic and force all output files into a single format, leaving it to the user to ensure the output is valid for their runtime.
205207

206-
Node.js supports both CommonJS and ECMAScript modules, with specific rules for which format each file can be and how the two formats are allowed to interoperate. `node16` and `nodenext` describe the full range of behavior for Node.js’s dual-format module system, and **emit files in either CommonJS or ESM format**. This is different from every other `module` option, which are runtime-agnostic and force all output files into a single format, leaving it to the user to ensure the output is valid for their runtime.
208+
> A common misconception is that `node16``nodenext` only emit ES modules. In reality, these modes describe versions of Node.js that _support_ ES modules, not just projects that _use_ ES modules. Both ESM and CommonJS emit are supported, based on the [detected module format](#module-format-detection) of each file. Because they are the only `module` options that reflect the complexities of Node.js’s dual module system, they are the **only correct `module` options** for all apps and libraries that are intended to run in Node.js v12 or later, whether they use ES modules or not.
207209
208-
> A common misconception is that `node16` and `nodenext` only emit ES modules. In reality, `node16` and `nodenext` describe versions of Node.js that _support_ ES modules, not just projects that _use_ ES modules. Both ESM and CommonJS emit are supported, based on the [detected module format](#module-format-detection) of each file. Because `node16` and `nodenext` are the only `module` options that reflect the complexities of Node.js’s dual module system, they are the **only correct `module` options** for all apps and libraries that are intended to run in Node.js v12 or later, whether they use ES modules or not.
210+
The fixed-version `node16` and `node18` modes represent the module system behavior stabilized in their respective Node.js versions, while the `nodenext` mode changes with the latest stable versions of Node.js. The following table summarizes the current differences between the three modes:
209211

210-
`node16` and `nodenext` are currently identical, with the exception that they [imply different `target` option values](#implied-and-enforced-options). If Node.js makes significant changes to its module system in the future, `node16` will be frozen while `nodenext` will be updated to reflect the new behavior.
212+
| | `target` | `moduleResolution` | import assertions | import attributes | JSON imports | require(esm) |
213+
|----------|----------|--------------------|-------------------|-------------------|---------------------|--------------|
214+
| node16 | `es2022` | `node16` ||| no restrictions ||
215+
| node18 | `es2022` | `node16` ||| needs `type "json"` ||
216+
| nodenext | `esnext` | `nodenext` ||| needs `type "json"` ||
211217

212218
#### Module format detection
213219

@@ -223,8 +229,9 @@ The detected module format of input `.ts`/`.tsx`/`.mts`/`.cts` files determines
223229
- The `module.exports` of the CommonJS module is available as a default import to the ES module.
224230
- Properties (other than `default`) of the CommonJS module’s `module.exports` may or may not be available as named imports to the ES module. Node.js attempts to make them available via [static analysis](https://github.com/nodejs/cjs-module-lexer). TypeScript cannot know from a declaration file whether that static analysis will succeed, and optimistically assumes it will. This limits TypeScript’s ability to catch named imports that may crash at runtime. See [#54018](https://github.com/microsoft/TypeScript/issues/54018) for more details.
225231
- **When a CommonJS module references an ES module:**
226-
- `require` cannot reference an ES module. For TypeScript, this includes `import` statements in files that are [detected](#module-format-detection) to be CommonJS modules, since those `import` statements will be transformed to `require` calls in the emitted JavaScript.
227-
- A dynamic `import()` call may be used to import an ES module. It returns a Promise of the module’s Module Namespace Object (what you’d get from `import * as ns from "./module.js"` from another ES module).
232+
- In `node16` and `node18`, `require` cannot reference an ES module. For TypeScript, this includes `import` statements in files that are [detected](#module-format-detection) to be CommonJS modules, since those `import` statements will be transformed to `require` calls in the emitted JavaScript.
233+
- In `nodenext`, to reflect the behavior of Node.js v22.12.0 and later, `require` can reference an ES module. In Node.js, an error is thrown if the ES module, or any of its imported modules, uses top-level `await`. TypeScript does not attempt to detect this case and will not emit a compile-time error. The result of the `require` call is the module’s Module Namespace Object, i.e., the same as the result of an `await import()` of the same module (but without the need to `await` anything).
234+
- A dynamic `import()` call can always be used to import an ES module. It returns a Promise of the module’s Module Namespace Object (what you’d get from `import * as ns from "./module.js"` from another ES module).
228235

229236
#### Emit
230237

@@ -263,15 +270,16 @@ const dynamic = import("mod"); // not transformed
263270
264271
#### Implied and enforced options
265272
266-
- `--module nodenext` or `node16` implies and enforces the `moduleResolution` with the same name.
273+
- `--module nodenext` implies and enforces `--moduleResolution nodenext`.
274+
- `--module node18` or `node16` implies and enforces `--moduleResolution node16`.
267275
- `--module nodenext` implies `--target esnext`.
268-
- `--module node16` implies `--target es2022`.
269-
- `--module nodenext` or `node16` implies `--esModuleInterop`.
276+
- `--module node18` or `node16` implies `--target es2022`.
277+
- `--module nodenext` or `node18` or `node16` implies `--esModuleInterop`.
270278
271279
#### Summary
272280
273-
- `node16` and `nodenext` are the only correct `module` options for all apps and libraries that are intended to run in Node.js v12 or later, whether they use ES modules or not.
274-
- `node16` and `nodenext` emit files in either CommonJS or ESM format, based on the [detected module format](#module-format-detection) of each file.
281+
- `node16`, `node18`, and `nodenext` are the only correct `module` options for all apps and libraries that are intended to run in Node.js v12 or later, whether they use ES modules or not.
282+
- `node16`, `node18`, and `nodenext` emit files in either CommonJS or ESM format, based on the [detected module format](#module-format-detection) of each file.
275283
- Node.js’s interoperability rules between ESM and CJS are reflected in type checking.
276284
- ESM emit transforms `import x = require("...")` to a `require` call constructed from a `createRequire` import.
277285
- CommonJS emit leaves dynamic `import()` calls untransformed, so CommonJS modules can asynchronously import ES modules.
@@ -316,7 +324,7 @@ export default "default export";
316324
#### Summary
317325
318326
- Use `esnext` with `--moduleResolution bundler` for bundlers, Bun, and tsx.
319-
- Do not use for Node.js. Use `node16` or `nodenext` with `"type": "module"` in package.json to emit ES modules for Node.js.
327+
- Do not use for Node.js. Use `node16`, `node18`, or `nodenext` with `"type": "module"` in package.json to emit ES modules for Node.js.
320328
- `import mod = require("mod")` is not allowed in non-declaration files.
321329
- `es2020` adds support for `import.meta` properties.
322330
- `es2022` adds support for top-level `await`.
@@ -351,7 +359,7 @@ export default "default export";
351359
352360
#### Summary
353361
354-
- You probably shouldn’t use this. Use `node16` or `nodenext` to emit CommonJS modules for Node.js.
362+
- You probably shouldn’t use this. Use `node16`, `node18`, or `nodenext` to emit CommonJS modules for Node.js.
355363
- Emitted files are CommonJS modules, but dependencies may be any format.
356364
- Dynamic `import()` is transformed to a Promise of a `require()` call.
357365
- `esModuleInterop` affects the output code for default and namespace imports.
@@ -1111,7 +1119,7 @@ import mod = require("./mod"); // `require` algorithm due to syntax (emit
11111119

11121120
#### Implied and enforced options
11131121

1114-
- `--moduleResolution node16` and `nodenext` must be paired with their [corresponding `module` value](#node16-nodenext).
1122+
- `--moduleResolution node16` and `nodenext` must be paired with [`--module node16`, `node18`, or `nodenext`](#node16-node18-nodenext).
11151123

11161124
#### Supported features
11171125

0 commit comments

Comments
 (0)