Skip to content

Commit cc4bef8

Browse files
joyeecheungCeres6
authored andcommitted
module: trim off internal stack frames for require(esm) warnings
Trim off irrelevant internal stack frames for require(esm) warnings so it's easier to locate where the call comes from when --trace-warnings is used. PR-URL: nodejs#55496 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 165146f commit cc4bef8

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

lib/internal/modules/cjs/loader.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,10 @@ function loadESMFromCJS(mod, filename) {
13891389
messagePrefix = `${from} is loading ES Module ${to} using require().\n`;
13901390
}
13911391
}
1392-
emitExperimentalWarning('Support for loading ES Module in require()', messagePrefix);
1392+
emitExperimentalWarning('Support for loading ES Module in require()',
1393+
messagePrefix,
1394+
undefined,
1395+
parent?.require);
13931396
const {
13941397
wrap,
13951398
namespace,

lib/internal/util.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,20 @@ function slowCases(enc) {
266266
}
267267
}
268268

269-
function emitExperimentalWarning(feature, messagePrefix) {
269+
/**
270+
* @param {string} feature Feature name used in the warning message
271+
* @param {string} messagePrefix Prefix of the warning message
272+
* @param {string} code See documentation of process.emitWarning
273+
* @param {string} ctor See documentation of process.emitWarning
274+
*/
275+
function emitExperimentalWarning(feature, messagePrefix, code, ctor) {
270276
if (experimentalWarnings.has(feature)) return;
271277
experimentalWarnings.add(feature);
272278
let msg = `${feature} is an experimental feature and might change at any time`;
273279
if (messagePrefix) {
274280
msg = messagePrefix + msg;
275281
}
276-
process.emitWarning(msg, 'ExperimentalWarning');
282+
process.emitWarning(msg, 'ExperimentalWarning', code, ctor);
277283
}
278284

279285
function filterDuplicateStrings(items, low) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
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+
6+
require('../common');
7+
const { spawnSyncAndAssert } = require('../common/child_process');
8+
const fixtures = require('../common/fixtures');
9+
const assert = require('assert');
10+
11+
spawnSyncAndAssert(process.execPath, [
12+
'--trace-warnings',
13+
fixtures.path('es-modules', 'require-module.js'),
14+
], {
15+
trim: true,
16+
stderr(output) {
17+
const lines = output.split('\n');
18+
assert.match(
19+
lines[0],
20+
/ExperimentalWarning: CommonJS module .*require-module\.js is loading ES Module .*message\.mjs/
21+
);
22+
assert.strictEqual(
23+
lines[1],
24+
'Support for loading ES Module in require() is an experimental feature and might change at any time'
25+
);
26+
assert.match(
27+
lines[2],
28+
/at require \(.*modules\/helpers:\d+:\d+\)/
29+
);
30+
assert.match(
31+
lines[3],
32+
/at Object\.<anonymous> \(.*require-module\.js:1:1\)/
33+
);
34+
}
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./message.mjs');

0 commit comments

Comments
 (0)