module: include module information in require(esm) warning · nodejs/node@8192dd6 · GitHub
Skip to content

Commit 8192dd6

Browse files
joyeecheungmarco-ippolito
authored andcommitted
module: include module information in require(esm) warning
When emitting the experimental warning for `require(esm)`, include information about the parent module and the module being require()-d to help users locate and update them. PR-URL: #55397 Backport-PR-URL: #56927 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Refs: #52697
1 parent 1db210a commit 8192dd6

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 22 additions & 2 deletions

lib/internal/util.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,13 @@ function slowCases(enc) {
255255
}
256256
}
257257

258-
function emitExperimentalWarning(feature) {
258+
function emitExperimentalWarning(feature, messagePrefix) {
259259
if (experimentalWarnings.has(feature)) return;
260-
const msg = `${feature} is an experimental feature and might change at any time`;
261260
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+
}
262265
process.emitWarning(msg, 'ExperimentalWarning');
263266
}
264267

test/es-module/test-require-module-preload.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ require('../common');
44
const { spawnSyncAndAssert } = require('../common/child_process');
55
const { fixturesDir } = require('../common/fixtures');
66

7-
const warningRE = /ExperimentalWarning: Support for loading ES Module in require/;
87
function testPreload(preloadFlag) {
98
// The warning is only emitted when ESM is loaded by --require.
10-
const stderr = preloadFlag !== '--import' ? warningRE : undefined;
11-
9+
const isRequire = preloadFlag === '--require';
1210
// Test named exports.
1311
{
1412
spawnSyncAndAssert(
@@ -24,7 +22,8 @@ function testPreload(preloadFlag) {
2422
},
2523
{
2624
stdout: 'A',
27-
stderr,
25+
stderr: isRequire ?
26+
/ExperimentalWarning: --require is loading ES Module .*module-named-exports\.mjs using require/ : undefined,
2827
trim: true,
2928
}
3029
);
@@ -44,7 +43,8 @@ function testPreload(preloadFlag) {
4443
cwd: fixturesDir
4544
},
4645
{
47-
stderr,
46+
stderr: isRequire ?
47+
/ExperimentalWarning: --require is loading ES Module .*import-esm\.mjs using require/ : undefined,
4848
stdout: /^world\s+A$/,
4949
trim: true,
5050
}
@@ -66,7 +66,8 @@ function testPreload(preloadFlag) {
6666
},
6767
{
6868
stdout: /^ok\s+A$/,
69-
stderr,
69+
stderr: isRequire ?
70+
/ExperimentalWarning: --require is loading ES Module .*cjs-exports\.mjs using require/ : undefined,
7071
trim: true,
7172
}
7273
);
@@ -89,7 +90,8 @@ function testPreload(preloadFlag) {
8990
},
9091
{
9192
stdout: /^world\s+A$/,
92-
stderr,
93+
stderr: isRequire ?
94+
/ExperimentalWarning: --require is loading ES Module .*require-cjs\.mjs using require/ : undefined,
9395
trim: true,
9496
}
9597
);
@@ -115,7 +117,7 @@ testPreload('--import');
115117
},
116118
{
117119
stdout: /^package-type-module\s+A$/,
118-
stderr: warningRE,
120+
stderr: /ExperimentalWarning: --require is loading ES Module .*package-type-module[\\/]index\.js using require/,
119121
trim: true,
120122
}
121123
);

test/es-module/test-require-module.js

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)