@@ -14,6 +14,7 @@ const {
14
14
AggregateError,
15
15
ArrayFrom,
16
16
ArrayIsArray,
17
+ ArrayPrototypeFilter,
17
18
ArrayPrototypeIncludes,
18
19
ArrayPrototypeIndexOf,
19
20
ArrayPrototypeJoin,
@@ -788,6 +789,34 @@ const fatalExceptionStackEnhancers = {
788
789
}
789
790
} ;
790
791
792
+ // Ensures the printed error line is from user code.
793
+ let _kArrowMessagePrivateSymbol , _setHiddenValue ;
794
+ function setArrowMessage ( err , arrowMessage ) {
795
+ if ( ! _kArrowMessagePrivateSymbol ) {
796
+ ( {
797
+ arrow_message_private_symbol : _kArrowMessagePrivateSymbol ,
798
+ setHiddenValue : _setHiddenValue ,
799
+ } = internalBinding ( 'util' ) ) ;
800
+ }
801
+ _setHiddenValue ( err , _kArrowMessagePrivateSymbol , arrowMessage ) ;
802
+ }
803
+
804
+ // Hide stack lines before the first user code line.
805
+ function hideInternalStackFrames ( error ) {
806
+ overrideStackTrace . set ( error , ( error , stackFrames ) => {
807
+ let frames = stackFrames ;
808
+ if ( typeof stackFrames === 'object' ) {
809
+ frames = ArrayPrototypeFilter (
810
+ stackFrames ,
811
+ ( frm ) => ! StringPrototypeStartsWith ( frm . getFileName ( ) ,
812
+ 'node:internal' )
813
+ ) ;
814
+ }
815
+ ArrayPrototypeUnshift ( frames , error ) ;
816
+ return ArrayPrototypeJoin ( frames , '\n at ' ) ;
817
+ } ) ;
818
+ }
819
+
791
820
// Node uses an AbortError that isn't exactly the same as the DOMException
792
821
// to make usage of the error in userland and readable-stream easier.
793
822
// It is a regular error with `.code` and `.name`.
@@ -806,8 +835,10 @@ module.exports = {
806
835
exceptionWithHostPort,
807
836
getMessage,
808
837
hideStackFrames,
838
+ hideInternalStackFrames,
809
839
isErrorStackTraceLimitWritable,
810
840
isStackOverflowError,
841
+ setArrowMessage,
811
842
connResetException,
812
843
uvErrmapGet,
813
844
uvException,
@@ -842,6 +873,7 @@ module.exports = {
842
873
// Note: Please try to keep these in alphabetical order
843
874
//
844
875
// Note: Node.js specific errors must begin with the prefix ERR_
876
+
845
877
E ( 'ERR_AMBIGUOUS_ARGUMENT' , 'The "%s" argument is ambiguous. %s' , TypeError ) ;
846
878
E ( 'ERR_ARG_NOT_ITERABLE' , '%s must be iterable' , TypeError ) ;
847
879
E ( 'ERR_ASSERTION' , '%s' , Error ) ;
@@ -1406,23 +1438,32 @@ E('ERR_PERFORMANCE_INVALID_TIMESTAMP',
1406
1438
'%d is not a valid timestamp' , TypeError ) ;
1407
1439
E ( 'ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS' , '%s' , TypeError ) ;
1408
1440
E ( 'ERR_REQUIRE_ESM' ,
1409
- ( filename , parentPath = null , packageJsonPath = null ) => {
1410
- let msg = `Must use import to load ES Module: ${ filename } ` ;
1411
- if ( parentPath && packageJsonPath ) {
1412
- const path = require ( 'path' ) ;
1413
- const basename = path . basename ( filename ) === path . basename ( parentPath ) ?
1414
- filename : path . basename ( filename ) ;
1415
- msg +=
1416
- '\nrequire() of ES modules is not supported.\nrequire() of ' +
1417
- `${ filename } from ${ parentPath } ` +
1418
- 'is an ES module file as it is a .js file whose nearest parent ' +
1419
- 'package.json contains "type": "module" which defines all .js ' +
1420
- 'files in that package scope as ES modules.\nInstead rename ' +
1421
- `${ basename } to end in .cjs, change the requiring code to use ` +
1422
- 'import(), or remove "type": "module" from ' +
1423
- `${ packageJsonPath } .\n` ;
1441
+ function ( filename , hasEsmSyntax , parentPath = null , packageJsonPath = null ) {
1442
+ hideInternalStackFrames ( this ) ;
1443
+ let msg = `require() of ES Module ${ filename } ${ parentPath ? ` from ${
1444
+ parentPath } ` : '' } not supported.`;
1445
+ if ( ! packageJsonPath ) {
1446
+ if ( StringPrototypeEndsWith ( filename , '.mjs' ) )
1447
+ msg += `\nInstead change the require of ${ filename } to a dynamic ` +
1448
+ 'import() which is available in all CommonJS modules.' ;
1449
+ return msg ;
1450
+ }
1451
+ const path = require ( 'path' ) ;
1452
+ const basename = path . basename ( filename ) === path . basename ( parentPath ) ?
1453
+ filename : path . basename ( filename ) ;
1454
+ if ( hasEsmSyntax ) {
1455
+ msg += `\nInstead change the require of ${ basename } in ${ parentPath } to` +
1456
+ ' a dynamic import() which is available in all CommonJS modules.' ;
1424
1457
return msg ;
1425
1458
}
1459
+ msg += `\n${ basename } is treated as an ES module file as it is a .js ` +
1460
+ 'file whose nearest parent package.json contains "type": "module" ' +
1461
+ 'which declares all .js files in that package scope as ES modules.' +
1462
+ `\nInstead rename ${ basename } to end in .cjs, change the requiring ` +
1463
+ 'code to use dynamic import() which is available in all CommonJS ' +
1464
+ 'modules, or change "type": "module" to "type": "commonjs" in ' +
1465
+ `${ packageJsonPath } to treat all .js files as CommonJS (using .mjs for ` +
1466
+ 'all ES modules instead).\n' ;
1426
1467
return msg ;
1427
1468
} , Error ) ;
1428
1469
E ( 'ERR_SCRIPT_EXECUTION_INTERRUPTED' ,
0 commit comments