Skip to content

Commit 94f237e

Browse files
juanarboltargos
authored andcommitted
lib,test: improves ERR_REQUIRE_ESM message
PR-URL: #30694Fixes: #30599 Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 30756e3 commit 94f237e

4 files changed

Lines changed: 35 additions & 28 deletions

File tree

‎lib/internal/errors.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,26 @@ E('ERR_OUT_OF_RANGE',
11231123
msg+=` It must be ${range}. Received ${received}`;
11241124
returnmsg;
11251125
},RangeError);
1126-
E('ERR_REQUIRE_ESM','Must use import to load ES Module: %s',Error);
1126+
E('ERR_REQUIRE_ESM',
1127+
(filename,parentPath=null,packageJsonPath=null)=>{
1128+
letmsg=`Must use import to load ES Module: ${filename}`;
1129+
if(parentPath&&packageJsonPath){
1130+
constpath=require('path');
1131+
constbasename=path.basename(filename)===path.basename(parentPath) ?
1132+
filename : path.basename(filename);
1133+
msg+=
1134+
'\nrequire() of ES modules is not supported.\nrequire() of '+
1135+
`${filename}${parentPath ? `from ${parentPath} ` : ''}`+
1136+
'is an ES module file as it is a .js file whose nearest parent '+
1137+
'package.json contains "type": "module" which defines all .js '+
1138+
'files in that package scope as ES modules.\nInstead rename '+
1139+
`${basename} to end in .cjs, change the requiring code to use `+
1140+
'import(), or remove "type": "module" from '+
1141+
`${packageJsonPath}.\n`;
1142+
returnmsg;
1143+
}
1144+
returnmsg;
1145+
},Error);
11271146
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
11281147
'Script execution was interrupted by `SIGINT`',Error);
11291148
E('ERR_SERVER_ALREADY_LISTEN',

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,33 +1139,14 @@ Module.prototype._compile = function(content, filename) {
11391139
};
11401140

11411141
// Native extension for .js
1142-
letwarnRequireESM=true;
11431142
Module._extensions['.js']=function(module,filename){
11441143
if(filename.endsWith('.js')){
11451144
constpkg=readPackageScope(filename);
1145+
// Function require shouldn't be used in ES modules.
11461146
if(pkg&&pkg.data&&pkg.data.type==='module'){
1147-
if(warnRequireESM){
1148-
constparentPath=module.parent&&module.parent.filename;
1149-
constbasename=parentPath&&
1150-
path.basename(filename)===path.basename(parentPath) ?
1151-
filename : path.basename(filename);
1152-
process.emitWarning(
1153-
'require() of ES modules is not supported.\nrequire() of '+
1154-
`${filename}${parentPath ? `from ${module.parent.filename} ` : ''}`+
1155-
'is an ES module file as it is a .js file whose nearest parent '+
1156-
'package.json contains "type": "module" which defines all .js '+
1157-
'files in that package scope as ES modules.\nInstead rename '+
1158-
`${basename} to end in .cjs, change the requiring code to use `+
1159-
'import(), or remove "type": "module" from '+
1160-
`${path.resolve(pkg.path,'package.json')}.`,
1161-
undefined,
1162-
undefined,
1163-
undefined,
1164-
true
1165-
);
1166-
warnRequireESM=false;
1167-
}
1168-
thrownewERR_REQUIRE_ESM(filename);
1147+
constparentPath=module.parent&&module.parent.filename;
1148+
constpackageJsonPath=path.resolve(pkg.path,'package.json');
1149+
thrownewERR_REQUIRE_ESM(filename,parentPath,packageJsonPath);
11691150
}
11701151
}
11711152
constcontent=fs.readFileSync(filename,'utf8');

‎test/es-module/test-cjs-esm-warn.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ child.on('close', common.mustCall((code, signal) => {
2626
assert.strictEqual(code,1);
2727
assert.strictEqual(signal,null);
2828

29-
assert.ok(stderr.startsWith(`(node:${child.pid}) Warning: `+
30-
'require() of ES modules is not supported.\nrequire() of '+
29+
assert.ok(stderr.indexOf(
30+
`Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ${required}`+
31+
'\nrequire() of ES modules is not supported.\nrequire() of '+
3132
`${required} from ${requiring} `+
3233
'is an ES module file as it is a .js file whose nearest parent '+
3334
'package.json contains "type": "module" which defines all .js '+
3435
'files in that package scope as ES modules.\nInstead rename '+
3536
`${basename} to end in .cjs, change the requiring code to use `+
3637
'import(), or remove "type": "module" from '+
37-
`${pjson}.\n`));
38+
`${pjson}.\n`)!==-1);
3839
assert.ok(stderr.indexOf(
3940
'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module')!==-1);
41+
42+
assert.strictEqual(
43+
stderr.match(/MustuseimporttoloadESModule/g).length,1);
4044
}));

‎test/es-module/test-esm-type-flag-errors.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ try {
2727
require('../fixtures/es-modules/package-type-module/index.js');
2828
assert.fail('Expected CJS to fail loading from type: module package.');
2929
}catch(e){
30-
assert(e.toString().match(/Error\[ERR_REQUIRE_ESM\]:MustuseimporttoloadESModule:/));
30+
assert.strictEqual(e.name,'Error');
31+
assert.strictEqual(e.code,'ERR_REQUIRE_ESM');
32+
assert(e.toString().match(/MustuseimporttoloadESModule/g));
33+
assert(e.message.match(/MustuseimporttoloadESModule/g));
3134
}
3235

3336
functionexpect(opt='',inputFile,want,wantsError=false){

0 commit comments

Comments
 (0)