Skip to content

Commit 16119f2

Browse files
joyeecheungruyadorno
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: #55496 Backport-PR-URL: #55217 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Refs: #52697
1 parent 28b5b9a commit 16119f2

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,10 @@ function loadESMFromCJS(mod, filename) {
13941394
messagePrefix=`${from} is loading ES Module ${to} using require().\n`;
13951395
}
13961396
}
1397-
emitExperimentalWarning('Support for loading ES Module in require()',messagePrefix);
1397+
emitExperimentalWarning('Support for loading ES Module in require()',
1398+
messagePrefix,
1399+
undefined,
1400+
parent?.require);
13981401
const{
13991402
wrap,
14001403
namespace,

‎lib/internal/util.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,20 @@ function slowCases(enc) {
256256
}
257257
}
258258

259-
functionemitExperimentalWarning(feature,messagePrefix){
259+
/**
260+
* @param {string} feature Feature name used in the warning message
261+
* @param {string} messagePrefix Prefix of the warning message
262+
* @param {string} code See documentation of process.emitWarning
263+
* @param {string} ctor See documentation of process.emitWarning
264+
*/
265+
functionemitExperimentalWarning(feature,messagePrefix,code,ctor){
260266
if(experimentalWarnings.has(feature))return;
261267
experimentalWarnings.add(feature);
262268
letmsg=`${feature} is an experimental feature and might change at any time`;
263269
if(messagePrefix){
264270
msg=messagePrefix+msg;
265271
}
266-
process.emitWarning(msg,'ExperimentalWarning');
272+
process.emitWarning(msg,'ExperimentalWarning',code,ctor);
267273
}
268274

269275
functionfilterDuplicateStrings(items,low){
Lines changed: 35 additions & 0 deletions
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+
constfixtures=require('../common/fixtures');
9+
constassert=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+
constlines=output.split('\n');
18+
assert.match(
19+
lines[0],
20+
/ExperimentalWarning:CommonJSmodule.*require-module\.jsisloadingESModule.*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+
/atrequire\(.*modules\/helpers:\d+:\d+\)/
29+
);
30+
assert.match(
31+
lines[3],
32+
/atObject\.<anonymous>\(.*require-module\.js:1:1\)/
33+
);
34+
}
35+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./message.mjs');

0 commit comments

Comments
 (0)