@@ -1195,29 +1195,39 @@ let warnRequireESM = true;
1195
1195
Module . _extensions [ '.js' ] = function ( module , filename ) {
1196
1196
if ( filename . endsWith ( '.js' ) ) {
1197
1197
const pkg = readPackageScope ( filename ) ;
1198
+ // Function require shouldn't be used in ES modules.
1198
1199
if ( pkg && pkg . data && pkg . data . type === 'module' ) {
1200
+ const err = new ERR_REQUIRE_ESM ( filename ) ;
1199
1201
if ( warnRequireESM ) {
1200
1202
const parentPath = module . parent && module . parent . filename ;
1201
1203
const basename = parentPath &&
1202
1204
path . basename ( filename ) === path . basename ( parentPath ) ?
1203
1205
filename : path . basename ( filename ) ;
1204
- process . emitWarning (
1206
+
1207
+ const warningMsg =
1205
1208
'require() of ES modules is not supported.\nrequire() of ' +
1206
1209
`${ filename } ${ parentPath ? `from ${ module . parent . filename } ` : '' } ` +
1207
1210
'is an ES module file as it is a .js file whose nearest parent ' +
1208
1211
'package.json contains "type": "module" which defines all .js ' +
1209
1212
'files in that package scope as ES modules.\nInstead rename ' +
1210
1213
`${ basename } to end in .cjs, change the requiring code to use ` +
1211
1214
'import(), or remove "type": "module" from ' +
1212
- `${ path . resolve ( pkg . path , 'package.json' ) } .` ,
1215
+ `${ path . resolve ( pkg . path , 'package.json' ) } .\n` +
1216
+ err . message ;
1217
+
1218
+ // The error message should be (in this case) the as the warning.
1219
+ // https://github.com/nodejs/node/issues/30599
1220
+ err . message = warningMsg ;
1221
+ process . emitWarning (
1222
+ warningMsg ,
1213
1223
undefined ,
1214
1224
undefined ,
1215
1225
undefined ,
1216
1226
true
1217
1227
) ;
1218
1228
warnRequireESM = false ;
1219
1229
}
1220
- throw new ERR_REQUIRE_ESM ( filename ) ;
1230
+ throw err ;
1221
1231
}
1222
1232
}
1223
1233
const content = fs . readFileSync ( filename , 'utf8' ) ;
0 commit comments