Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v20.x] backport unflagging of require(esm) to v20 #56927

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b3e865a
module: eliminate performance cost of detection for cjs entry
GeoffreyBooth Mar 20, 2024
cf363a4
module: detect ESM syntax by trying to recompile as SourceTextModule
joyeecheung Apr 19, 2024
5477382
lib,src: iterate module requests of a module wrap in JS
legendecas Apr 25, 2024
cb4d3a6
module: support ESM detection in the CJS loader
joyeecheung Apr 29, 2024
b6268a2
module: cache synchronous module jobs before linking
joyeecheung May 7, 2024
1af4525
module: do not set CJS variables for Worker eval
aduh95 May 25, 2024
9bc8eab
lib: allow CJS source map cache to be reclaimed
legendecas May 28, 2024
e337526
lib: reduce amount of caught URL errors
anonrig Jun 13, 2024
07b24a3
src: refactor embedded entrypoint loading
joyeecheung Jul 5, 2024
ad274c7
module: add __esModule to require()'d ESM
joyeecheung Mar 20, 2024
f6b0e4e
esm: refactor `get_format`
aduh95 Jul 18, 2024
bea73c8
module: unflag detect-module
GeoffreyBooth Jul 20, 2024
4e3963f
module: do not warn for typeless package.json when there isn't one
joyeecheung Jul 29, 2024
11f03fa
esm: throw `ERR_REQUIRE_ESM` instead of `ERR_INTERNAL_ASSERTION`
aduh95 Sep 11, 2024
3348ece
module: remove bogus assertion in CJS entrypoint handling with --import
joyeecheung Aug 27, 2024
03c5801
module: report unfinished TLA in ambiguous modules
aduh95 Sep 19, 2024
5885691
module: implement the "module-sync" exports condition
joyeecheung Sep 25, 2024
8e684e3
module: refator ESM loader for adding future synchronous hooks
joyeecheung Sep 17, 2024
4ab3aff
module: unflag --experimental-require-module
joyeecheung Sep 26, 2024
824d417
module: support 'module.exports' interop export in require(esm)
guybedford Jul 14, 2024
6ab6126
doc: update `require(ESM)` history and stability status
aduh95 Oct 2, 2024
b641ee2
module: use kNodeModulesRE to detect node_modules
joyeecheung Oct 4, 2024
f801d34
process: add process.features.require_module
joyeecheung Oct 7, 2024
6b14716
src: implement IsInsideNodeModules() in C++
joyeecheung Oct 8, 2024
714e88e
module: check --experimental-require-module separately from detection
joyeecheung Oct 8, 2024
5d78d74
module: include module information in require(esm) warning
joyeecheung Oct 22, 2024
948a513
module: allow ESM that failed to be required to be re-imported
joyeecheung Oct 28, 2024
9f8f935
module: trim off internal stack frames for require(esm) warnings
joyeecheung Oct 18, 2024
3bd3c18
module: fix error thrown from require(esm) hitting TLA repeatedly
joyeecheung Oct 29, 2024
f6fc5c6
module: do not warn when require(esm) comes from node_modules
joyeecheung Nov 23, 2024
43449f8
module: mark evaluation rejection in require(esm) as handled
joyeecheung Dec 5, 2024
7f0a829
module: only emit require(esm) warning under --trace-require-module
joyeecheung Dec 9, 2024
93ebd4d
module: disable require(esm) for policy and network import
joyeecheung Feb 6, 2025
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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ module.exports = {
'test/es-module/test-esm-example-loader.js',
'test/es-module/test-esm-type-flag.js',
'test/es-module/test-esm-type-flag-alias.js',
'test/es-module/test-require-module-detect-entry-point.js',
'test/es-module/test-require-module-detect-entry-point-aou.js',
],
parserOptions: { sourceType: 'module' },
},
Expand Down
1 change: 1 addition & 0 deletions benchmark/misc/startup-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const bench = common.createBenchmark(main, {
script: [
'benchmark/fixtures/require-builtins',
'test/fixtures/semicolon',
'test/fixtures/snapshot/typescript',
],
mode: ['process', 'worker'],
n: [30],
Expand Down
91 changes: 55 additions & 36 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -887,38 +887,6 @@
WebAssembly magic number (`\0asm`); otherwise they will be treated as ES module
JavaScript.

### `--experimental-detect-module`

<!-- YAML
added:
- v20.10.0
-->

> Stability: 1.1 - Active development

Node.js will inspect the source code of ambiguous input to determine whether it
contains ES module syntax; if such syntax is detected, the input will be treated
as an ES module.

Ambiguous input is defined as:

* Files with a `.js` extension or no extension; and either no controlling
`package.json` file or one that lacks a `type` field; and
`--experimental-default-type` is not specified.
* String input (`--eval` or STDIN) when neither `--input-type` nor
`--experimental-default-type` are specified.

ES module syntax is defined as syntax that would throw when evaluated as
CommonJS. This includes the following:

* `import` statements (but _not_ `import()` expressions, which are valid in
CommonJS).
* `export` statements.
* `import.meta` references.
* `await` at the top level of a module.
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
`exports`, `__dirname`, `__filename`).

### `--experimental-eventsource`

<!-- YAML
Expand Down Expand Up @@ -1024,7 +992,13 @@
### `--experimental-require-module`

<!-- YAML
added: v20.17.0
added:
- v22.0.0
- v20.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55085

Check warning on line 1000 in doc/api/cli.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: This is now true by default.
-->

> Stability: 1.1 - Active Development
Expand Down Expand Up @@ -1540,6 +1514,21 @@

Silence deprecation warnings.

### `--no-experimental-detect-module`

<!-- YAML
added:
- v21.1.0
- v20.10.0
changes:
- version:
- REPLACEME
pr-url: https://github.com/nodejs/node/pull/53619

Check warning on line 1526 in doc/api/cli.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: Syntax detection is enabled by default.
-->

Disable using [syntax detection][] to determine module type.

### `--no-experimental-fetch`

<!-- YAML
Expand Down Expand Up @@ -1572,6 +1561,24 @@

Use this flag to disable top-level await in REPL.

### `--no-experimental-require-module`

<!-- YAML
added:
- v22.0.0
- v20.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55085

Check warning on line 1572 in doc/api/cli.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: This is now false by default.
-->

> Stability: 1.1 - Active Development

Disable support for loading a synchronous ES module graph in `require()`.

See [Loading ECMAScript modules using `require()`][].

### `--no-extra-info-on-fatal-exception`

<!-- YAML
Expand Down Expand Up @@ -1781,9 +1788,7 @@
added: v20.17.0
-->

This flag is only useful when `--experimental-require-module` is enabled.

If the ES module being `require()`'d contains top-level await, this flag
If the ES module being `require()`'d contains top-level `await`, this flag
allows Node.js to evaluate the module, try to locate the
top-level awaits, and print their location to help users find them.

Expand Down Expand Up @@ -2382,6 +2387,18 @@
Prints a stack trace whenever an environment is exited proactively,
i.e. invoking `process.exit()`.

### `--trace-require-module=mode`

<!-- YAML
added:
- REPLACEME
-->

Prints information about usage of [Loading ECMAScript modules using `require()`][].

When `mode` is `all`, all usage is printed. When `mode` is `no-node-modules`, usage
from the `node_modules` folder is excluded.

### `--trace-sigint`

<!-- YAML
Expand Down Expand Up @@ -2860,6 +2877,7 @@
* `--trace-event-file-pattern`
* `--trace-events-enabled`
* `--trace-exit`
* `--trace-require-module`
* `--trace-sigint`
* `--trace-sync-io`
* `--trace-tls`
Expand Down Expand Up @@ -3384,6 +3402,7 @@
[security warning]: #warning-binding-inspector-to-a-public-ipport-combination-is-insecure
[semi-space]: https://www.memorymanagement.org/glossary/s.html#semi.space
[single executable application]: single-executable-applications.md
[syntax detection]: packages.md#syntax-detection
[test reporters]: test.md#test-reporters
[timezone IDs]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
[tracking issue for user-land snapshots]: https://github.com/nodejs/node/issues/44014
Expand Down
23 changes: 16 additions & 7 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2535,8 +2535,8 @@

> Stability: 1 - Experimental

When trying to `require()` a [ES Module][] under `--experimental-require-module`,
a CommonJS to ESM or ESM to CommonJS edge participates in an immediate cycle.
When trying to `require()` a [ES Module][], a CommonJS to ESM or ESM to CommonJS edge
participates in an immediate cycle.
This is not allowed because ES Modules cannot be evaluated while they are
already being evaluated.

Expand All @@ -2550,8 +2550,8 @@

> Stability: 1 - Experimental

When trying to `require()` a [ES Module][] under `--experimental-require-module`,
the module turns out to be asynchronous. That is, it contains top-level await.
When trying to `require()` a [ES Module][], the module turns out to be asynchronous.
That is, it contains top-level await.

To see where the top-level await is, use
`--experimental-print-required-tla` (this would execute the modules
Expand All @@ -2561,12 +2561,20 @@

### `ERR_REQUIRE_ESM`

> Stability: 1 - Experimental
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55085

Check warning on line 2567 in doc/api/errors.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: require() now supports loading synchronous ES modules by default.
-->

> Stability: 0 - Deprecated

An attempt was made to `require()` an [ES Module][].

To enable `require()` for synchronous module graphs (without
top-level `await`), use `--experimental-require-module`.
This error has been deprecated since `require()` now supports loading synchronous
ES modules. When `require()` encounters an ES module that contains top-level
`await`, it will throw [`ERR_REQUIRE_ASYNC_MODULE`][] instead.

<a id="ERR_SCRIPT_EXECUTION_INTERRUPTED"></a>

Expand Down Expand Up @@ -3908,6 +3916,7 @@
[`ERR_INVALID_ARG_TYPE`]: #err_invalid_arg_type
[`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: #err_missing_message_port_in_transfer_list
[`ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`]: #err_missing_transferable_in_transfer_list
[`ERR_REQUIRE_ASYNC_MODULE`]: #err_require_async_module
[`EventEmitter`]: events.md#class-eventemitter
[`MessagePort`]: worker_threads.md#class-messageport
[`Object.getPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
Expand Down
8 changes: 3 additions & 5 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ compatibility.
### `require`
The CommonJS module `require` currently only supports loading synchronous ES
modules when `--experimental-require-module` is enabled.
modules (that is, ES modules that do not use top-level `await`).
See [Loading ECMAScript modules using `require()`][] for details.
Expand Down Expand Up @@ -1071,8 +1071,7 @@ _isImports_, _conditions_)
> 10. If _url_ ends in _".js"_, then
> 1. If _packageType_ is not **null**, then
> 1. Return _packageType_.
> 2. If `--experimental-detect-module` is enabled and the result of
> **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
> 2. If the result of **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
> 1. Return _"module"_.
> 3. Return _"commonjs"_.
> 11. If _url_ does not have any extension, then
Expand All @@ -1082,8 +1081,7 @@ _isImports_, _conditions_)
> 1. Return _"wasm"_.
> 2. If _packageType_ is not **null**, then
> 1. Return _packageType_.
> 3. If `--experimental-detect-module` is enabled and the source of
> module contains static import or export syntax, then
> 3. If the result of **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
> 1. Return _"module"_.
> 4. Return _"commonjs"_.
> 12. Return **undefined** (will throw during load phase).
Expand Down
Loading
Loading