-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
PR-URL: #39175 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ const { | |
AggregateError, | ||
ArrayFrom, | ||
ArrayIsArray, | ||
ArrayPrototypeFilter, | ||
ArrayPrototypeIncludes, | ||
ArrayPrototypeIndexOf, | ||
ArrayPrototypeJoin, | ||
|
@@ -788,6 +789,34 @@ const fatalExceptionStackEnhancers = { | |
} | ||
}; | ||
|
||
// Ensures the printed error line is from user code. | ||
let _kArrowMessagePrivateSymbol, _setHiddenValue; | ||
function setArrowMessage(err, arrowMessage) { | ||
if (!_kArrowMessagePrivateSymbol) { | ||
({ | ||
arrow_message_private_symbol: _kArrowMessagePrivateSymbol, | ||
setHiddenValue: _setHiddenValue, | ||
} = internalBinding('util')); | ||
} | ||
_setHiddenValue(err, _kArrowMessagePrivateSymbol, arrowMessage); | ||
} | ||
|
||
// Hide stack lines before the first user code line. | ||
function hideInternalStackFrames(error) { | ||
overrideStackTrace.set(error, (error, stackFrames) => { | ||
let frames = stackFrames; | ||
if (typeof stackFrames === 'object') { | ||
frames = ArrayPrototypeFilter( | ||
stackFrames, | ||
(frm) => !StringPrototypeStartsWith(frm.getFileName(), | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
guybedford
Author
Contributor
|
||
'node:internal') | ||
); | ||
} | ||
ArrayPrototypeUnshift(frames, error); | ||
return ArrayPrototypeJoin(frames, '\n at '); | ||
}); | ||
} | ||
|
||
// Node uses an AbortError that isn't exactly the same as the DOMException | ||
// to make usage of the error in userland and readable-stream easier. | ||
// It is a regular error with `.code` and `.name`. | ||
|
@@ -806,8 +835,10 @@ module.exports = { | |
exceptionWithHostPort, | ||
getMessage, | ||
hideStackFrames, | ||
hideInternalStackFrames, | ||
isErrorStackTraceLimitWritable, | ||
isStackOverflowError, | ||
setArrowMessage, | ||
connResetException, | ||
uvErrmapGet, | ||
uvException, | ||
|
@@ -842,6 +873,7 @@ module.exports = { | |
// Note: Please try to keep these in alphabetical order | ||
// | ||
// Note: Node.js specific errors must begin with the prefix ERR_ | ||
|
||
E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError); | ||
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError); | ||
E('ERR_ASSERTION', '%s', Error); | ||
|
@@ -1406,23 +1438,32 @@ E('ERR_PERFORMANCE_INVALID_TIMESTAMP', | |
'%d is not a valid timestamp', TypeError); | ||
E('ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS', '%s', TypeError); | ||
E('ERR_REQUIRE_ESM', | ||
(filename, parentPath = null, packageJsonPath = null) => { | ||
let msg = `Must use import to load ES Module: ${filename}`; | ||
if (parentPath && packageJsonPath) { | ||
const path = require('path'); | ||
const basename = path.basename(filename) === path.basename(parentPath) ? | ||
filename : path.basename(filename); | ||
msg += | ||
'\nrequire() of ES modules is not supported.\nrequire() of ' + | ||
`${filename} from ${parentPath} ` + | ||
'is an ES module file as it is a .js file whose nearest parent ' + | ||
'package.json contains "type": "module" which defines all .js ' + | ||
'files in that package scope as ES modules.\nInstead rename ' + | ||
`${basename} to end in .cjs, change the requiring code to use ` + | ||
'import(), or remove "type": "module" from ' + | ||
`${packageJsonPath}.\n`; | ||
function(filename, hasEsmSyntax, parentPath = null, packageJsonPath = null) { | ||
hideInternalStackFrames(this); | ||
let msg = `require() of ES Module ${filename}${parentPath ? ` from ${ | ||
parentPath}` : ''} not supported.`; | ||
if (!packageJsonPath) { | ||
if (StringPrototypeEndsWith(filename, '.mjs')) | ||
msg += `\nInstead change the require of ${filename} to a dynamic ` + | ||
'import() which is available in all CommonJS modules.'; | ||
return msg; | ||
} | ||
const path = require('path'); | ||
const basename = path.basename(filename) === path.basename(parentPath) ? | ||
filename : path.basename(filename); | ||
if (hasEsmSyntax) { | ||
msg += `\nInstead change the require of ${basename} in ${parentPath} to` + | ||
' a dynamic import() which is available in all CommonJS modules.'; | ||
return msg; | ||
} | ||
msg += `\n${basename} is treated as an ES module file as it is a .js ` + | ||
'file whose nearest parent package.json contains "type": "module" ' + | ||
'which declares all .js files in that package scope as ES modules.' + | ||
`\nInstead rename ${basename} to end in .cjs, change the requiring ` + | ||
'code to use dynamic import() which is available in all CommonJS ' + | ||
'modules, or change "type": "module" to "type": "commonjs" in ' + | ||
`${packageJsonPath} to treat all .js files as CommonJS (using .mjs for ` + | ||
'all ES modules instead).\n'; | ||
return msg; | ||
}, Error); | ||
E('ERR_SCRIPT_EXECUTION_INTERRUPTED', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
const { | ||
ArrayPrototypeForEach, | ||
ArrayPrototypeJoin, | ||
ArrayPrototypeSome, | ||
ObjectDefineProperty, | ||
ObjectPrototypeHasOwnProperty, | ||
SafeMap, | ||
|
@@ -184,9 +185,26 @@ function normalizeReferrerURL(referrer) { | |
return new URL(referrer).href; | ||
} | ||
|
||
// For error messages only - used to check if ESM syntax is in use. | ||
function hasEsmSyntax(code) { | ||
const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser; | ||
This comment has been minimized.
Sorry, something went wrong.
privatenumber
Contributor
|
||
let root; | ||
try { | ||
root = parser.parse(code, { sourceType: 'module', ecmaVersion: 'latest' }); | ||
} catch { | ||
return false; | ||
} | ||
|
||
return ArrayPrototypeSome(root.body, (stmt) => | ||
stmt.type === 'ExportNamedDeclaration' || | ||
stmt.type === 'ImportDeclaration' || | ||
stmt.type === 'ExportAllDeclaration'); | ||
} | ||
|
||
module.exports = { | ||
addBuiltinLibsToObject, | ||
cjsConditions, | ||
hasEsmSyntax, | ||
loadNativeModule, | ||
makeRequireFunction, | ||
normalizeReferrerURL, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./package-type-module/esm.js'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export var p = 5; |
i think frm.getFileName() can return null or undefined here which causes a
TypeError: String.prototype.startsWith called on null or undefined
rather than a
ERR_REQUIRE_ESM
which breaks things like loading webpack.config.js in current releasehttps://github.com/webpack/webpack-cli/blob/a660ffcbeb2807bce1554f787297e697464abd59/packages/webpack-cli/lib/webpack-cli.js#L50-L51