@@ -14,6 +14,7 @@ const {
1414 AggregateError,
1515 ArrayFrom,
1616 ArrayIsArray,
17+ ArrayPrototypeFilter,
1718 ArrayPrototypeIncludes,
1819 ArrayPrototypeIndexOf,
1920 ArrayPrototypeJoin,
@@ -788,6 +789,34 @@ const fatalExceptionStackEnhancers = {
788789 }
789790} ;
790791
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+
791820// Node uses an AbortError that isn't exactly the same as the DOMException
792821// to make usage of the error in userland and readable-stream easier.
793822// It is a regular error with `.code` and `.name`.
@@ -806,8 +835,10 @@ module.exports = {
806835 exceptionWithHostPort,
807836 getMessage,
808837 hideStackFrames,
838+ hideInternalStackFrames,
809839 isErrorStackTraceLimitWritable,
810840 isStackOverflowError,
841+ setArrowMessage,
811842 connResetException,
812843 uvErrmapGet,
813844 uvException,
@@ -842,6 +873,7 @@ module.exports = {
842873// Note: Please try to keep these in alphabetical order
843874//
844875// Note: Node.js specific errors must begin with the prefix ERR_
876+
845877E ( 'ERR_AMBIGUOUS_ARGUMENT' , 'The "%s" argument is ambiguous. %s' , TypeError ) ;
846878E ( 'ERR_ARG_NOT_ITERABLE' , '%s must be iterable' , TypeError ) ;
847879E ( 'ERR_ASSERTION' , '%s' , Error ) ;
@@ -1406,23 +1438,32 @@ E('ERR_PERFORMANCE_INVALID_TIMESTAMP',
14061438 '%d is not a valid timestamp' , TypeError ) ;
14071439E ( 'ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS' , '%s' , TypeError ) ;
14081440E ( '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.' ;
14241457 return msg ;
14251458 }
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' ;
14261467 return msg ;
14271468 } , Error ) ;
14281469E ( 'ERR_SCRIPT_EXECUTION_INTERRUPTED' ,
0 commit comments