| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bfd11d7 commit 7cbe3de
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2669,6 +2669,18 @@ added: | |||
| 2669 | 2669 | Prints a stack trace whenever an environment is exited proactively, | |
| 2670 | 2670 | i.e. invoking `process.exit()`. | |
| 2671 | 2671 | ||
| 2672 | + ### `--trace-require-module=mode` | ||
| 2673 | + | ||
| 2674 | + <!-- YAML | ||
| 2675 | + added: | ||
| 2676 | + - REPLACEME | ||
| 2677 | + --> | ||
| 2678 | + | ||
| 2679 | + Prints information about usage of [Loading ECMAScript modules using `require()`][]. | ||
| 2680 | + | ||
| 2681 | + When `mode` is `all`, all usage is printed. When `mode` is `no-node-modules`, usage | ||
| 2682 | + from the `node_modules` folder is excluded. | ||
| 2683 | + | ||
| 2672 | 2684 | ### `--trace-sigint` | |
| 2673 | 2685 | ||
| 2674 | 2686 | <!-- YAML | |
@@ -3180,6 +3192,7 @@ one is included in the list below. | |||
| 3180 | 3192 | * `--trace-event-file-pattern` | |
| 3181 | 3193 | * `--trace-events-enabled` | |
| 3182 | 3194 | * `--trace-exit` | |
| 3195 | + * `--trace-require-module` | ||
| 3183 | 3196 | * `--trace-sigint` | |
| 3184 | 3197 | * `--trace-sync-io` | |
| 3185 | 3198 | * `--trace-tls` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -174,9 +174,15 @@ relative, and based on the real path of the files making the calls to | |||
| 174 | 174 | added: | |
| 175 | 175 | - v22.0.0 | |
| 176 | 176 | changes: | |
| 177 | - - version: v22.12.0 | ||
| 177 | + - version: | ||
| 178 | + - v23.0.0 | ||
| 179 | + - v22.12.0 | ||
| 178 | 180 | pr-url: https://github.com/nodejs/node/pull/55085 | |
| 179 | 181 | description: This feature is no longer behind the `--experimental-require-module` CLI flag. | |
| 182 | + - version: REPLACEME | ||
| 183 | + pr-url: https://github.com/nodejs/node/pull/56194 | ||
| 184 | + description: This feature no longer emits an experimental warning by default, | ||
| 185 | + though the warning can still be emitted by --trace-require-module. | ||
| 180 | 186 | - version: v22.12.0 | |
| 181 | 187 | pr-url: https://github.com/nodejs/node/pull/54563 | |
| 182 | 188 | description: Support `'module.exports'` interop export in `require(esm)`. | |
@@ -314,9 +320,8 @@ help users fix them. | |||
| 314 | 320 | ||
| 315 | 321 | Support for loading ES modules using `require()` is currently | |
| 316 | 322 | experimental and can be disabled using `--no-experimental-require-module`. | |
| 317 | - When `require()` actually encounters an ES module for the | ||
| 318 | - first time in the process, it will emit an experimental warning. The | ||
| 319 | - warning is expected to be removed when this feature stablizes. | ||
| 323 | + To print where this feature is used, use [`--trace-require-module`][]. | ||
| 324 | + | ||
| 320 | 325 | This feature can be detected by checking if | |
| 321 | 326 | [`process.features.require_module`][] is `true`. | |
| 322 | 327 | ||
@@ -1266,6 +1271,7 @@ This section was moved to | |||
| 1266 | 1271 | [GLOBAL_FOLDERS]: #loading-from-the-global-folders | |
| 1267 | 1272 | [`"main"`]: packages.md#main | |
| 1268 | 1273 | [`"type"`]: packages.md#type | |
| 1274 | + [`--trace-require-module`]: cli.md#--trace-require-modulemode | ||
| 1269 | 1275 | [`ERR_REQUIRE_ASYNC_MODULE`]: errors.md#err_require_async_module | |
| 1270 | 1276 | [`ERR_UNSUPPORTED_DIR_IMPORT`]: errors.md#err_unsupported_dir_import | |
| 1271 | 1277 | [`MODULE_NOT_FOUND`]: errors.md#module_not_found | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1338,7 +1338,7 @@ Module.prototype.require = function(id) { | |||
| 1338 | 1338 | } | |
| 1339 | 1339 | }; | |
| 1340 | 1340 | ||
| 1341 | - let emittedRequireModuleWarning = false; | ||
| 1341 | + let requireModuleWarningMode; | ||
| 1342 | 1342 | /** | |
| 1343 | 1343 | * Resolve and evaluate it synchronously as ESM if it's ESM. | |
| 1344 | 1344 | * @param {Module} mod CJS module instance | |
@@ -1361,17 +1361,22 @@ function loadESMFromCJS(mod, filename) { | |||
| 1361 | 1361 | } else { | |
| 1362 | 1362 | const parent = mod[kModuleParent]; | |
| 1363 | 1363 | ||
| 1364 | - if (!emittedRequireModuleWarning) { | ||
| 1364 | + requireModuleWarningMode ??= getOptionValue('--trace-require-module'); | ||
| 1365 | + if (requireModuleWarningMode) { | ||
| 1365 | 1366 | let shouldEmitWarning = false; | |
| 1366 | - // Check if the require() comes from node_modules. | ||
| 1367 | - if (parent) { | ||
| 1368 | - shouldEmitWarning = !isUnderNodeModules(parent.filename); | ||
| 1369 | - } else if (mod[kIsCachedByESMLoader]) { | ||
| 1370 | - // It comes from the require() built for `import cjs` and doesn't have a parent recorded | ||
| 1371 | - // in the CJS module instance. Inspect the stack trace to see if the require() | ||
| 1372 | - // comes from node_modules and reduce the noise. If there are more than 100 frames, | ||
| 1373 | - // just give up and assume it is under node_modules. | ||
| 1374 | - shouldEmitWarning = !isInsideNodeModules(100, true); | ||
| 1367 | + if (requireModuleWarningMode === 'no-node-modules') { | ||
| 1368 | + // Check if the require() comes from node_modules. | ||
| 1369 | + if (parent) { | ||
| 1370 | + shouldEmitWarning = !isUnderNodeModules(parent.filename); | ||
| 1371 | + } else if (mod[kIsCachedByESMLoader]) { | ||
| 1372 | + // It comes from the require() built for `import cjs` and doesn't have a parent recorded | ||
| 1373 | + // in the CJS module instance. Inspect the stack trace to see if the require() | ||
| 1374 | + // comes from node_modules and reduce the noise. If there are more than 100 frames, | ||
| 1375 | + // just give up and assume it is under node_modules. | ||
| 1376 | + shouldEmitWarning = !isInsideNodeModules(100, true); | ||
| 1377 | + } | ||
| 1378 | + } else { | ||
| 1379 | + shouldEmitWarning = true; | ||
| 1375 | 1380 | } | |
| 1376 | 1381 | if (shouldEmitWarning) { | |
| 1377 | 1382 | let messagePrefix; | |
@@ -1397,7 +1402,7 @@ function loadESMFromCJS(mod, filename) { | |||
| 1397 | 1402 | messagePrefix, | |
| 1398 | 1403 | undefined, | |
| 1399 | 1404 | parent?.require); | |
| 1400 | - emittedRequireModuleWarning = true; | ||
| 1405 | + requireModuleWarningMode = true; | ||
| 1401 | 1406 | } | |
| 1402 | 1407 | } | |
| 1403 | 1408 | const { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -142,6 +142,11 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors, | |||
| 142 | 142 | errors->push_back("--heapsnapshot-near-heap-limit must not be negative"); | |
| 143 | 143 | } | |
| 144 | 144 | ||
| 145 | + if (!trace_require_module.empty() && trace_require_module != "all" && | ||
| 146 | + trace_require_module != "no-node-modules") { | ||
| 147 | + errors->push_back("invalid value for --trace-require-module"); | ||
| 148 | + } | ||
| 149 | + | ||
| 145 | 150 | if (test_runner) { | |
| 146 | 151 | if (test_isolation == "none") { | |
| 147 | 152 | debug_options_.allow_attaching_debugger = true; | |
@@ -797,6 +802,14 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | |||
| 797 | 802 | "set module system to use by default", | |
| 798 | 803 | &EnvironmentOptions::type, | |
| 799 | 804 | kAllowedInEnvvar); | |
| 805 | + | ||
| 806 | + AddOption( | ||
| 807 | + "--trace-require-module", | ||
| 808 | + "Print access to require(esm). Options are 'all' (print all usage) and " | ||
| 809 | + "'no-node-modules' (excluding usage from the node_modules folder)", | ||
| 810 | + &EnvironmentOptions::trace_require_module, | ||
| 811 | + kAllowedInEnvvar); | ||
| 812 | + | ||
| 800 | 813 | AddOption("--extra-info-on-fatal-exception", | |
| 801 | 814 | "hide extra information on fatal exception that causes exit", | |
| 802 | 815 | &EnvironmentOptions::extra_info_on_fatal_exception, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -213,6 +213,7 @@ class EnvironmentOptions : public Options { | |||
| 213 | 213 | bool trace_env = false; | |
| 214 | 214 | bool trace_env_js_stack = false; | |
| 215 | 215 | bool trace_env_native_stack = false; | |
| 216 | + std::string trace_require_module; | ||
| 216 | 217 | bool extra_info_on_fatal_exception = true; | |
| 217 | 218 | std::string unhandled_rejections; | |
| 218 | 219 | std::vector<std::string> userland_loaders; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,8 +5,6 @@ const { spawnSyncAndAssert } = require('../common/child_process'); | |||
| 5 | 5 | const { fixturesDir } = require('../common/fixtures'); | |
| 6 | 6 | ||
| 7 | 7 | function testPreload(preloadFlag) { | |
| 8 | - // The warning is only emitted when ESM is loaded by --require. | ||
| 9 | - const isRequire = preloadFlag === '--require'; | ||
| 10 | 8 | // Test named exports. | |
| 11 | 9 | { | |
| 12 | 10 | spawnSyncAndAssert( | |
@@ -22,8 +20,6 @@ function testPreload(preloadFlag) { | |||
| 22 | 20 | }, | |
| 23 | 21 | { | |
| 24 | 22 | stdout: 'A', | |
| 25 | - stderr: isRequire ? | ||
| 26 | - /ExperimentalWarning: --require is loading ES Module .*module-named-exports\.mjs using require/ : undefined, | ||
| 27 | 23 | trim: true, | |
| 28 | 24 | } | |
| 29 | 25 | ); | |
@@ -43,8 +39,6 @@ function testPreload(preloadFlag) { | |||
| 43 | 39 | cwd: fixturesDir | |
| 44 | 40 | }, | |
| 45 | 41 | { | |
| 46 | - stderr: isRequire ? | ||
| 47 | - /ExperimentalWarning: --require is loading ES Module .*import-esm\.mjs using require/ : undefined, | ||
| 48 | 42 | stdout: /^world\s+A$/, | |
| 49 | 43 | trim: true, | |
| 50 | 44 | } | |
@@ -66,8 +60,6 @@ function testPreload(preloadFlag) { | |||
| 66 | 60 | }, | |
| 67 | 61 | { | |
| 68 | 62 | stdout: /^ok\s+A$/, | |
| 69 | - stderr: isRequire ? | ||
| 70 | - /ExperimentalWarning: --require is loading ES Module .*cjs-exports\.mjs using require/ : undefined, | ||
| 71 | 63 | trim: true, | |
| 72 | 64 | } | |
| 73 | 65 | ); | |
@@ -90,8 +82,6 @@ function testPreload(preloadFlag) { | |||
| 90 | 82 | }, | |
| 91 | 83 | { | |
| 92 | 84 | stdout: /^world\s+A$/, | |
| 93 | - stderr: isRequire ? | ||
| 94 | - /ExperimentalWarning: --require is loading ES Module .*require-cjs\.mjs using require/ : undefined, | ||
| 95 | 85 | trim: true, | |
| 96 | 86 | } | |
| 97 | 87 | ); | |
@@ -117,7 +107,6 @@ testPreload('--import'); | |||
| 117 | 107 | }, | |
| 118 | 108 | { | |
| 119 | 109 | stdout: /^package-type-module\s+A$/, | |
| 120 | - stderr: /ExperimentalWarning: --require is loading ES Module .*package-type-module[\\/]index\.js using require/, | ||
| 121 | 110 | trim: true, | |
| 122 | 111 | } | |
| 123 | 112 | ); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,14 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - // This checks the warning and the stack trace emitted by the require(esm) | ||
| 4 | - // experimental warning. It can get removed when `require(esm)` becomes stable. | ||
| 5 | - | ||
| 3 | + // This checks the warning and the stack trace emitted by --trace-require-module=all. | ||
| 6 | 4 | require('../common'); | |
| 7 | 5 | const { spawnSyncAndAssert } = require('../common/child_process'); | |
| 8 | 6 | const fixtures = require('../common/fixtures'); | |
| 9 | 7 | const assert = require('assert'); | |
| 10 | 8 | ||
| 11 | 9 | spawnSyncAndAssert(process.execPath, [ | |
| 12 | 10 | '--trace-warnings', | |
| 11 | + '--trace-require-module=all', | ||
| 13 | 12 | fixtures.path('es-modules', 'require-module.js'), | |
| 14 | 13 | ], { | |
| 15 | 14 | trim: true, | |
@@ -33,3 +32,12 @@ spawnSyncAndAssert(process.execPath, [ | |||
| 33 | 32 | ); | |
| 34 | 33 | } | |
| 35 | 34 | }); | |
| 35 | + | ||
| 36 | + spawnSyncAndAssert(process.execPath, [ | ||
| 37 | + '--trace-require-module=1', | ||
| 38 | + fixtures.path('es-modules', 'require-module.js'), | ||
| 39 | + ], { | ||
| 40 | + status: 9, | ||
| 41 | + trim: true, | ||
| 42 | + stderr: /invalid value for --trace-require-module/ | ||
| 43 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,16 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | 5 | const assert = require('assert'); | |
| 6 | - const path = require('path'); | ||
| 7 | - | ||
| 8 | - // Only the first load will trigger the warning. | ||
| 9 | - common.expectWarning( | ||
| 10 | - 'ExperimentalWarning', | ||
| 11 | - `CommonJS module ${__filename} is loading ES Module ` + | ||
| 12 | - `${path.resolve(__dirname, '../fixtures/es-module-loaders/module-named-exports.mjs')} using require().\n` + | ||
| 13 | - 'Support for loading ES Module in require() is an experimental feature ' + | ||
| 14 | - 'and might change at any time' | ||
| 15 | - ); | ||
| 16 | 6 | ||
| 17 | 7 | // Test named exports. | |
| 18 | 8 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - // This checks the experimental warning for require(esm) is disabled when the | ||
| 4 | - // require() comes from node_modules. | ||
| 3 | + // This checks the warning and the stack trace emitted by | ||
| 4 | + // --trace-require-module=no-node-modules. | ||
| 5 | 5 | require('../common'); | |
| 6 | 6 | const { spawnSyncAndAssert } = require('../common/child_process'); | |
| 7 | 7 | const fixtures = require('../common/fixtures'); | |
@@ -14,7 +14,10 @@ const warningRE = /Support for loading ES Module in require\(\)/; | |||
| 14 | 14 | // require() in non-node_modules -> esm in node_modules should warn. | |
| 15 | 15 | spawnSyncAndAssert( | |
| 16 | 16 | process.execPath, | |
| 17 | - [fixtures.path('es-modules', 'test_node_modules', 'require-esm.js')], | ||
| 17 | + [ | ||
| 18 | + '--trace-require-module=no-node-modules', | ||
| 19 | + fixtures.path('es-modules', 'test_node_modules', 'require-esm.js'), | ||
| 20 | + ], | ||
| 18 | 21 | { | |
| 19 | 22 | trim: true, | |
| 20 | 23 | stderr: warningRE, | |
@@ -26,7 +29,10 @@ spawnSyncAndAssert( | |||
| 26 | 29 | // should not warn. | |
| 27 | 30 | spawnSyncAndAssert( | |
| 28 | 31 | process.execPath, | |
| 29 | - [fixtures.path('es-modules', 'test_node_modules', 'require-require-esm.js')], | ||
| 32 | + [ | ||
| 33 | + '--trace-require-module=no-node-modules', | ||
| 34 | + fixtures.path('es-modules', 'test_node_modules', 'require-require-esm.js'), | ||
| 35 | + ], | ||
| 30 | 36 | { | |
| 31 | 37 | trim: true, | |
| 32 | 38 | stderr: '', | |
@@ -38,7 +44,10 @@ spawnSyncAndAssert( | |||
| 38 | 44 | // should not warn. | |
| 39 | 45 | spawnSyncAndAssert( | |
| 40 | 46 | process.execPath, | |
| 41 | - [fixtures.path('es-modules', 'test_node_modules', 'import-require-esm.mjs')], | ||
| 47 | + [ | ||
| 48 | + '--trace-require-module=no-node-modules', | ||
| 49 | + fixtures.path('es-modules', 'test_node_modules', 'import-require-esm.mjs'), | ||
| 50 | + ], | ||
| 42 | 51 | { | |
| 43 | 52 | trim: true, | |
| 44 | 53 | stderr: '', | |
@@ -50,7 +59,10 @@ spawnSyncAndAssert( | |||
| 50 | 59 | // require() in node_modules -> esm in node_modules should not warn. | |
| 51 | 60 | spawnSyncAndAssert( | |
| 52 | 61 | process.execPath, | |
| 53 | - [fixtures.path('es-modules', 'test_node_modules', 'import-import-require-esm.mjs')], | ||
| 62 | + [ | ||
| 63 | + '--trace-require-module=no-node-modules', | ||
| 64 | + fixtures.path('es-modules', 'test_node_modules', 'import-import-require-esm.mjs'), | ||
| 65 | + ], | ||
| 54 | 66 | { | |
| 55 | 67 | trim: true, | |
| 56 | 68 | stderr: '', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,7 +116,6 @@ test('execute a .cts file importing a .mts file export', async () => { | |||
| 116 | 116 | fixtures.path('typescript/cts/test-require-mts-module.cts'), | |
| 117 | 117 | ]); | |
| 118 | 118 | ||
| 119 | - match(result.stderr, /Support for loading ES Module in require\(\) is an experimental feature and might change at any time/); | ||
| 120 | 119 | match(result.stdout, /Hello, TypeScript!/); | |
| 121 | 120 | strictEqual(result.code, 0); | |
| 122 | 121 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments