| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 683c93f commit 1ac1dda
20 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -992,7 +992,13 @@ Use the specified file as a security policy. | |||
| 992 | 992 | ### `--experimental-require-module` | |
| 993 | 993 | ||
| 994 | 994 | <!-- YAML | |
| 995 | - added: v20.17.0 | ||
| 995 | + added: | ||
| 996 | + - v22.0.0 | ||
| 997 | + - v20.17.0 | ||
| 998 | + changes: | ||
| 999 | + - version: REPLACEME | ||
| 1000 | + pr-url: https://github.com/nodejs/node/pull/55085 | ||
| 1001 | + description: This is now true by default. | ||
| 996 | 1002 | --> | |
| 997 | 1003 | ||
| 998 | 1004 | > Stability: 1.1 - Active Development | |
@@ -1555,6 +1561,24 @@ added: v16.6.0 | |||
| 1555 | 1561 | ||
| 1556 | 1562 | Use this flag to disable top-level await in REPL. | |
| 1557 | 1563 | ||
| 1564 | + ### `--no-experimental-require-module` | ||
| 1565 | + | ||
| 1566 | + <!-- YAML | ||
| 1567 | + added: | ||
| 1568 | + - v22.0.0 | ||
| 1569 | + - v20.17.0 | ||
| 1570 | + changes: | ||
| 1571 | + - version: REPLACEME | ||
| 1572 | + pr-url: https://github.com/nodejs/node/pull/55085 | ||
| 1573 | + description: This is now false by default. | ||
| 1574 | + --> | ||
| 1575 | + | ||
| 1576 | + > Stability: 1.1 - Active Development | ||
| 1577 | + | ||
| 1578 | + Disable support for loading a synchronous ES module graph in `require()`. | ||
| 1579 | + | ||
| 1580 | + See [Loading ECMAScript modules using `require()`][]. | ||
| 1581 | + | ||
| 1558 | 1582 | ### `--no-extra-info-on-fatal-exception` | |
| 1559 | 1583 | ||
| 1560 | 1584 | <!-- YAML | |
@@ -1764,9 +1788,7 @@ Identical to `-e` but prints the result. | |||
| 1764 | 1788 | added: v20.17.0 | |
| 1765 | 1789 | --> | |
| 1766 | 1790 | ||
| 1767 | - This flag is only useful when `--experimental-require-module` is enabled. | ||
| 1768 | - | ||
| 1769 | - If the ES module being `require()`'d contains top-level await, this flag | ||
| 1791 | + If the ES module being `require()`'d contains top-level `await`, this flag | ||
| 1770 | 1792 | allows Node.js to evaluate the module, try to locate the | |
| 1771 | 1793 | top-level awaits, and print their location to help users find them. | |
| 1772 | 1794 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2535,8 +2535,8 @@ object. | |||
| 2535 | 2535 | ||
| 2536 | 2536 | > Stability: 1 - Experimental | |
| 2537 | 2537 | ||
| 2538 | - When trying to `require()` a [ES Module][] under `--experimental-require-module`, | ||
| 2539 | - a CommonJS to ESM or ESM to CommonJS edge participates in an immediate cycle. | ||
| 2538 | + When trying to `require()` a [ES Module][], a CommonJS to ESM or ESM to CommonJS edge | ||
| 2539 | + participates in an immediate cycle. | ||
| 2540 | 2540 | This is not allowed because ES Modules cannot be evaluated while they are | |
| 2541 | 2541 | already being evaluated. | |
| 2542 | 2542 | ||
@@ -2550,8 +2550,8 @@ module, and should be done lazily in an inner function. | |||
| 2550 | 2550 | ||
| 2551 | 2551 | > Stability: 1 - Experimental | |
| 2552 | 2552 | ||
| 2553 | - When trying to `require()` a [ES Module][] under `--experimental-require-module`, | ||
| 2554 | - the module turns out to be asynchronous. That is, it contains top-level await. | ||
| 2553 | + When trying to `require()` a [ES Module][], the module turns out to be asynchronous. | ||
| 2554 | + That is, it contains top-level await. | ||
| 2555 | 2555 | ||
| 2556 | 2556 | To see where the top-level await is, use | |
| 2557 | 2557 | `--experimental-print-required-tla` (this would execute the modules | |
@@ -2561,12 +2561,20 @@ before looking for the top-level awaits). | |||
| 2561 | 2561 | ||
| 2562 | 2562 | ### `ERR_REQUIRE_ESM` | |
| 2563 | 2563 | ||
| 2564 | - > Stability: 1 - Experimental | ||
| 2564 | + <!-- YAML | ||
| 2565 | + changes: | ||
| 2566 | + - version: REPLACEME | ||
| 2567 | + pr-url: https://github.com/nodejs/node/pull/55085 | ||
| 2568 | + description: require() now supports loading synchronous ES modules by default. | ||
| 2569 | + --> | ||
| 2570 | + | ||
| 2571 | + > Stability: 0 - Deprecated | ||
| 2565 | 2572 | ||
| 2566 | 2573 | An attempt was made to `require()` an [ES Module][]. | |
| 2567 | 2574 | ||
| 2568 | - To enable `require()` for synchronous module graphs (without | ||
| 2569 | - top-level `await`), use `--experimental-require-module`. | ||
| 2575 | + This error has been deprecated since `require()` now supports loading synchronous | ||
| 2576 | + ES modules. When `require()` encounters an ES module that contains top-level | ||
| 2577 | + `await`, it will throw [`ERR_REQUIRE_ASYNC_MODULE`][] instead. | ||
| 2570 | 2578 | ||
| 2571 | 2579 | <a id="ERR_SCRIPT_EXECUTION_INTERRUPTED"></a> | |
| 2572 | 2580 | ||
@@ -3908,6 +3916,7 @@ An error occurred trying to allocate memory. This should never happen. | |||
| 3908 | 3916 | [`ERR_INVALID_ARG_TYPE`]: #err_invalid_arg_type | |
| 3909 | 3917 | [`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: #err_missing_message_port_in_transfer_list | |
| 3910 | 3918 | [`ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`]: #err_missing_transferable_in_transfer_list | |
| 3919 | + [`ERR_REQUIRE_ASYNC_MODULE`]: #err_require_async_module | ||
| 3911 | 3920 | [`EventEmitter`]: events.md#class-eventemitter | |
| 3912 | 3921 | [`MessagePort`]: worker_threads.md#class-messageport | |
| 3913 | 3922 | [`Object.getPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -450,7 +450,7 @@ compatibility. | |||
| 450 | 450 | ### `require` | |
| 451 | 451 | ||
| 452 | 452 | The CommonJS module `require` currently only supports loading synchronous ES | |
| 453 | - modules when `--experimental-require-module` is enabled. | ||
| 453 | + modules (that is, ES modules that do not use top-level `await`). | ||
| 454 | 454 | ||
| 455 | 455 | See [Loading ECMAScript modules using `require()`][] for details. | |
| 456 | 456 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -172,20 +172,17 @@ relative, and based on the real path of the files making the calls to | |||
| 172 | 172 | ||
| 173 | 173 | <!-- YAML | |
| 174 | 174 | added: v20.17.0 | |
| 175 | + changes: | ||
| 176 | + - version: REPLACEME | ||
| 177 | + pr-url: https://github.com/nodejs/node/pull/55085 | ||
| 178 | + description: require() now supports loading synchronous ES modules by default. | ||
| 175 | 179 | --> | |
| 176 | 180 | ||
| 177 | - > Stability: 1.1 - Active Development. Enable this API with the | ||
| 178 | - > [`--experimental-require-module`][] CLI flag. | ||
| 179 | - | ||
| 180 | 181 | The `.mjs` extension is reserved for [ECMAScript Modules][]. | |
| 181 | - Currently, if the flag `--experimental-require-module` is not used, loading | ||
| 182 | - an ECMAScript module using `require()` will throw a [`ERR_REQUIRE_ESM`][] | ||
| 183 | - error, and users need to use [`import()`][] instead. See | ||
| 184 | - [Determining module system][] section for more info | ||
| 182 | + See [Determining module system][] section for more info | ||
| 185 | 183 | regarding which files are parsed as ECMAScript modules. | |
| 186 | 184 | ||
| 187 | - If `--experimental-require-module` is enabled, and the ECMAScript module being | ||
| 188 | - loaded by `require()` meets the following requirements: | ||
| 185 | + `require()` only supports loading ECMAScript modules that meet the following requirements: | ||
| 189 | 186 | ||
| 190 | 187 | * The module is fully synchronous (contains no top-level `await`); and | |
| 191 | 188 | * One of these conditions are met: | |
@@ -194,8 +191,8 @@ loaded by `require()` meets the following requirements: | |||
| 194 | 191 | 3. The file has a `.js` extension, the closest `package.json` does not contain | |
| 195 | 192 | `"type": "commonjs"`, and the module contains ES module syntax. | |
| 196 | 193 | ||
| 197 | - `require()` will load the requested module as an ES Module, and return | ||
| 198 | - the module namespace object. In this case it is similar to dynamic | ||
| 194 | + If the ES Module being loaded meet the requirements, `require()` can load it and | ||
| 195 | + return the module namespace object. In this case it is similar to dynamic | ||
| 199 | 196 | `import()` but is run synchronously and returns the name space object | |
| 200 | 197 | directly. | |
| 201 | 198 | ||
@@ -214,7 +211,7 @@ class Point { | |||
| 214 | 211 | export default Point; | |
| 215 | 212 | ``` | |
| 216 | 213 | ||
| 217 | - A CommonJS module can load them with `require()` under `--experimental-detect-module`: | ||
| 214 | + A CommonJS module can load them with `require()`: | ||
| 218 | 215 | ||
| 219 | 216 | ```cjs | |
| 220 | 217 | const distance = require('./distance.mjs'); | |
@@ -243,13 +240,19 @@ conventions. Code authored directly in CommonJS should avoid depending on it. | |||
| 243 | 240 | If the module being `require()`'d contains top-level `await`, or the module | |
| 244 | 241 | graph it `import`s contains top-level `await`, | |
| 245 | 242 | [`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users should | |
| 246 | - load the asynchronous module using `import()`. | ||
| 243 | + load the asynchronous module using [`import()`][]. | ||
| 247 | 244 | ||
| 248 | 245 | If `--experimental-print-required-tla` is enabled, instead of throwing | |
| 249 | 246 | `ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the | |
| 250 | 247 | module, try to locate the top-level awaits, and print their location to | |
| 251 | 248 | help users fix them. | |
| 252 | 249 | ||
| 250 | + Support for loading ES modules using `require()` is currently | ||
| 251 | + experimental and can be disabled using `--no-experimental-require-module`. | ||
| 252 | + When `require()` actually encounters an ES module for the | ||
| 253 | + first time in the process, it will emit an experimental warning. The | ||
| 254 | + warning is expected to be removed when this feature stablizes. | ||
| 255 | + | ||
| 253 | 256 | ## All together | |
| 254 | 257 | ||
| 255 | 258 | <!-- type=misc --> | |
@@ -279,8 +282,7 @@ require(X) from module at path Y | |||
| 279 | 282 | ||
| 280 | 283 | MAYBE_DETECT_AND_LOAD(X) | |
| 281 | 284 | 1. If X parses as a CommonJS module, load X as a CommonJS module. STOP. | |
| 282 | - 2. Else, if `--experimental-require-module` is | ||
| 283 | - enabled, and the source code of X can be parsed as ECMAScript module using | ||
| 285 | + 2. Else, if the source code of X can be parsed as ECMAScript module using | ||
| 284 | 286 | <a href="esm.md#resolver-algorithm-specification">DETECT_MODULE_SYNTAX defined in | |
| 285 | 287 | the ESM resolver</a>, | |
| 286 | 288 | a. Load X as an ECMAScript module. STOP. | |
@@ -1196,9 +1198,7 @@ This section was moved to | |||
| 1196 | 1198 | [GLOBAL_FOLDERS]: #loading-from-the-global-folders | |
| 1197 | 1199 | [`"main"`]: packages.md#main | |
| 1198 | 1200 | [`"type"`]: packages.md#type | |
| 1199 | - [`--experimental-require-module`]: cli.md#--experimental-require-module | ||
| 1200 | 1201 | [`ERR_REQUIRE_ASYNC_MODULE`]: errors.md#err_require_async_module | |
| 1201 | - [`ERR_REQUIRE_ESM`]: errors.md#err_require_esm | ||
| 1202 | 1202 | [`ERR_UNSUPPORTED_DIR_IMPORT`]: errors.md#err_unsupported_dir_import | |
| 1203 | 1203 | [`MODULE_NOT_FOUND`]: errors.md#module_not_found | |
| 1204 | 1204 | [`__dirname`]: #__dirname | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -172,8 +172,7 @@ There is the CommonJS module loader: | |||
| 172 | 172 | * It treats all files that lack `.json` or `.node` extensions as JavaScript | |
| 173 | 173 | text files. | |
| 174 | 174 | * It can only be used to [load ECMASCript modules from CommonJS modules][] if | |
| 175 | - the module graph is synchronous (that contains no top-level `await`) when | ||
| 176 | - `--experimental-require-module` is enabled. | ||
| 175 | + the module graph is synchronous (that contains no top-level `await`). | ||
| 177 | 176 | When used to load a JavaScript text file that is not an ECMAScript module, | |
| 178 | 177 | the file will be loaded as a CommonJS module. | |
| 179 | 178 | ||
@@ -662,8 +661,7 @@ specific to least specific as conditions should be defined: | |||
| 662 | 661 | * `"require"` - matches when the package is loaded via `require()`. The | |
| 663 | 662 | referenced file should be loadable with `require()` although the condition | |
| 664 | 663 | matches regardless of the module format of the target file. Expected | |
| 665 | - formats include CommonJS, JSON, native addons, and ES modules | ||
| 666 | - if `--experimental-require-module` is enabled. _Always mutually | ||
| 664 | + formats include CommonJS, JSON, native addons, and ES modules. _Always mutually | ||
| 667 | 665 | exclusive with `"import"`._ | |
| 668 | 666 | * `"module-sync"` - matches no matter the package is loaded via `import`, | |
| 669 | 667 | `import()` or `require()`. The format is expected to be ES modules that does | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -435,7 +435,6 @@ function initializeCJS() { | |||
| 435 | 435 | require('internal/modules/run_main').executeUserEntryPoint; | |
| 436 | 436 | ||
| 437 | 437 | if (getOptionValue('--experimental-require-module')) { | |
| 438 | - emitExperimentalWarning('Support for loading ES Module in require()'); | ||
| 439 | 438 | Module._extensions['.mjs'] = loadESMFromCJS; | |
| 440 | 439 | } | |
| 441 | 440 | } | |
@@ -1341,6 +1340,7 @@ function loadESMFromCJS(mod, filename) { | |||
| 1341 | 1340 | // ESM won't be accessible via process.mainModule. | |
| 1342 | 1341 | setOwnProperty(process, 'mainModule', undefined); | |
| 1343 | 1342 | } else { | |
| 1343 | + emitExperimentalWarning('Support for loading ES Module in require()'); | ||
| 1344 | 1344 | const { | |
| 1345 | 1345 | wrap, | |
| 1346 | 1346 | namespace, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,11 +152,6 @@ async function defaultLoad(url, context = kEmptyObject) { | |||
| 152 | 152 | ||
| 153 | 153 | validateAttributes(url, format, importAttributes); | |
| 154 | 154 | ||
| 155 | - // Use the synchronous commonjs translator which can deal with cycles. | ||
| 156 | - if (format === 'commonjs' && getOptionValue('--experimental-require-module')) { | ||
| 157 | - format = 'commonjs-sync'; | ||
| 158 | - } | ||
| 159 | - | ||
| 160 | 155 | return { | |
| 161 | 156 | __proto__: null, | |
| 162 | 157 | format, | |
@@ -206,11 +201,6 @@ function defaultLoadSync(url, context = kEmptyObject) { | |||
| 206 | 201 | ||
| 207 | 202 | validateAttributes(url, format, importAttributes); | |
| 208 | 203 | ||
| 209 | - // Use the synchronous commonjs translator which can deal with cycles. | ||
| 210 | - if (format === 'commonjs' && getOptionValue('--experimental-require-module')) { | ||
| 211 | - format = 'commonjs-sync'; | ||
| 212 | - } | ||
| 213 | - | ||
| 214 | 204 | return { | |
| 215 | 205 | __proto__: null, | |
| 216 | 206 | format, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -364,15 +364,15 @@ class ModuleLoader { | |||
| 364 | 364 | ||
| 365 | 365 | defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync; | |
| 366 | 366 | const loadResult = defaultLoadSync(url, { format, importAttributes }); | |
| 367 | - const { | ||
| 368 | - format: finalFormat, | ||
| 369 | - source, | ||
| 370 | - } = loadResult; | ||
| 367 | + | ||
| 368 | + // Use the synchronous commonjs translator which can deal with cycles. | ||
| 369 | + const finalFormat = loadResult.format === 'commonjs' ? 'commonjs-sync' : loadResult.format; | ||
| 371 | 370 | ||
| 372 | 371 | if (finalFormat === 'wasm') { | |
| 373 | 372 | assert.fail('WASM is currently unsupported by require(esm)'); | |
| 374 | 373 | } | |
| 375 | 374 | ||
| 375 | + const { source } = loadResult; | ||
| 376 | 376 | const isMain = (parentURL === undefined); | |
| 377 | 377 | const wrap = this.#translate(url, finalFormat, source, isMain); | |
| 378 | 378 | assert(wrap instanceof ModuleWrap, `Translator used for require(${url}) should not be async`); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -353,6 +353,7 @@ class ModuleJobSync extends ModuleJobBase { | |||
| 353 | 353 | } | |
| 354 | 354 | ||
| 355 | 355 | runSync() { | |
| 356 | + // TODO(joyeecheung): add the error decoration logic from the async instantiate. | ||
| 356 | 357 | this.module.instantiateSync(); | |
| 357 | 358 | setHasStartedUserESMExecution(); | |
| 358 | 359 | const namespace = this.module.evaluateSync(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,17 +75,15 @@ function initializeDefaultConditions() { | |||
| 75 | 75 | const userConditions = getOptionValue('--conditions'); | |
| 76 | 76 | const noAddons = getOptionValue('--no-addons'); | |
| 77 | 77 | const addonConditions = noAddons ? [] : ['node-addons']; | |
| 78 | - | ||
| 78 | + const moduleConditions = getOptionValue('--experimental-require-module') ? ['module-sync'] : []; | ||
| 79 | 79 | defaultConditions = ObjectFreeze([ | |
| 80 | 80 | 'node', | |
| 81 | 81 | 'import', | |
| 82 | + ...moduleConditions, | ||
| 82 | 83 | ...addonConditions, | |
| 83 | 84 | ...userConditions, | |
| 84 | 85 | ]); | |
| 85 | 86 | defaultConditionsSet = new SafeSet(defaultConditions); | |
| 86 | - if (getOptionValue('--experimental-require-module')) { | ||
| 87 | - defaultConditionsSet.add('module-sync'); | ||
| 88 | - } | ||
| 89 | 87 | } | |
| 90 | 88 | ||
| 91 | 89 | /** | |
| Back | FazBrowse Home | New Git URL |
0 commit comments