GeezerVison is showing github.com — module: include module information in require(esm) warning · nodejs/node@8192dd6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1340,11 +1340,31 @@ function loadESMFromCJS(mod, filename) { | 1340 | 1340 | // ESM won't be accessible via process.mainModule. | 1341 | 1341 | setOwnProperty(process, 'mainModule', undefined); | 1342 | 1342 | } else { | 1343 | | - emitExperimentalWarning('Support for loading ES Module in require()'); | | 1343 | + const parent = mod[kModuleParent]; | | 1344 | + let messagePrefix; | | 1345 | + if (parent) { | | 1346 | + // In the case of the module calling `require()`, it's more useful to know its absolute path. | | 1347 | + let from = parent.filename || parent.id; | | 1348 | + // In the case of the module being require()d, it's more useful to know the id passed into require(). | | 1349 | + const to = mod.id || mod.filename; | | 1350 | + if (from === 'internal/preload') { | | 1351 | + from = '--require'; | | 1352 | + } else if (from === '<repl>') { | | 1353 | + from = 'The REPL'; | | 1354 | + } else if (from === '.') { | | 1355 | + from = 'The entry point'; | | 1356 | + } else { | | 1357 | + from &&= `CommonJS module ${from}`; | | 1358 | + } | | 1359 | + if (from && to) { | | 1360 | + messagePrefix = `${from} is loading ES Module ${to} using require().\n`; | | 1361 | + } | | 1362 | + } | | 1363 | + emitExperimentalWarning('Support for loading ES Module in require()', messagePrefix); | 1344 | 1364 | const { | 1345 | 1365 | wrap, | 1346 | 1366 | namespace, | 1347 | | - } = cascadedLoader.importSyncForRequire(mod, filename, source, isMain, mod[kModuleParent]); | | 1367 | + } = cascadedLoader.importSyncForRequire(mod, filename, source, isMain, parent); | 1348 | 1368 | // Tooling in the ecosystem have been using the __esModule property to recognize | 1349 | 1369 | // transpiled ESM in consuming code. For example, a 'log' package written in ESM: | 1350 | 1370 | // | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -255,10 +255,13 @@ function slowCases(enc) { | 255 | 255 | } | 256 | 256 | } | 257 | 257 |
| 258 | | -function emitExperimentalWarning(feature) { | | 258 | +function emitExperimentalWarning(feature, messagePrefix) { | 259 | 259 | if (experimentalWarnings.has(feature)) return; | 260 | | - const msg = `${feature} is an experimental feature and might change at any time`; | 261 | 260 | experimentalWarnings.add(feature); | | 261 | + let msg = `${feature} is an experimental feature and might change at any time`; | | 262 | + if (messagePrefix) { | | 263 | + msg = messagePrefix + msg; | | 264 | + } | 262 | 265 | process.emitWarning(msg, 'ExperimentalWarning'); | 263 | 266 | } | 264 | 267 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-require-module-preload.js +10-8Lines changed: 10 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,11 +4,9 @@ require('../common'); | 4 | 4 | const { spawnSyncAndAssert } = require('../common/child_process'); | 5 | 5 | const { fixturesDir } = require('../common/fixtures'); | 6 | 6 |
| 7 | | -const warningRE = /ExperimentalWarning: Support for loading ES Module in require/; | 8 | 7 | function testPreload(preloadFlag) { | 9 | 8 | // The warning is only emitted when ESM is loaded by --require. | 10 | | - const stderr = preloadFlag !== '--import' ? warningRE : undefined; | 11 | | - | | 9 | + const isRequire = preloadFlag === '--require'; | 12 | 10 | // Test named exports. | 13 | 11 | { | 14 | 12 | spawnSyncAndAssert( | @@ -24,7 +22,8 @@ function testPreload(preloadFlag) { | 24 | 22 | }, | 25 | 23 | { | 26 | 24 | stdout: 'A', | 27 | | - stderr, | | 25 | + stderr: isRequire ? | | 26 | + /ExperimentalWarning: --require is loading ES Module .*module-named-exports\.mjs using require/ : undefined, | 28 | 27 | trim: true, | 29 | 28 | } | 30 | 29 | ); | @@ -44,7 +43,8 @@ function testPreload(preloadFlag) { | 44 | 43 | cwd: fixturesDir | 45 | 44 | }, | 46 | 45 | { | 47 | | - stderr, | | 46 | + stderr: isRequire ? | | 47 | + /ExperimentalWarning: --require is loading ES Module .*import-esm\.mjs using require/ : undefined, | 48 | 48 | stdout: /^world\s+A$/, | 49 | 49 | trim: true, | 50 | 50 | } | @@ -66,7 +66,8 @@ function testPreload(preloadFlag) { | 66 | 66 | }, | 67 | 67 | { | 68 | 68 | stdout: /^ok\s+A$/, | 69 | | - stderr, | | 69 | + stderr: isRequire ? | | 70 | + /ExperimentalWarning: --require is loading ES Module .*cjs-exports\.mjs using require/ : undefined, | 70 | 71 | trim: true, | 71 | 72 | } | 72 | 73 | ); | @@ -89,7 +90,8 @@ function testPreload(preloadFlag) { | 89 | 90 | }, | 90 | 91 | { | 91 | 92 | stdout: /^world\s+A$/, | 92 | | - stderr, | | 93 | + stderr: isRequire ? | | 94 | + /ExperimentalWarning: --require is loading ES Module .*require-cjs\.mjs using require/ : undefined, | 93 | 95 | trim: true, | 94 | 96 | } | 95 | 97 | ); | @@ -115,7 +117,7 @@ testPreload('--import'); | 115 | 117 | }, | 116 | 118 | { | 117 | 119 | stdout: /^package-type-module\s+A$/, | 118 | | - stderr: warningRE, | | 120 | + stderr: /ExperimentalWarning: --require is loading ES Module .*package-type-module[\\/]index\.js using require/, | 119 | 121 | trim: true, | 120 | 122 | } | 121 | 123 | ); | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-require-module.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | const common = require('../common'); | 5 | 5 | const assert = require('assert'); | | 6 | +const path = require('path'); | 6 | 7 |
| | 8 | +// Only the first load will trigger the warning. | 7 | 9 | common.expectWarning( | 8 | 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` + | 9 | 13 | 'Support for loading ES Module in require() is an experimental feature ' + | 10 | 14 | 'and might change at any time' | 11 | 15 | ); | |
|