-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
esm: refine ERR_REQUIRE_ESM errors #39175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
15babc4
8b6d578
0d129ba
7765a14
187c7b9
8fbf23b
fdd9b31
af548b5
cfe3db9
78640e4
16b687c
e628c0a
12b2009
8871f4e
dfd2397
b156244
cf5879f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- 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, | ||||||
|
|
@@ -337,7 +338,7 @@ function makeSystemErrorWithCode(key) { | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| function makeNodeErrorWithCode(Base, key) { | ||||||
| function makeNodeErrorWithCode(Base, key, deferStack) { | ||||||
| return function NodeError(...args) { | ||||||
| const limit = Error.stackTraceLimit; | ||||||
| if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0; | ||||||
|
|
@@ -389,18 +390,19 @@ function hideStackFrames(fn) { | |||||
| // Utility function for registering the error codes. Only used here. Exported | ||||||
| // *only* to allow for testing. | ||||||
| function E(sym, val, def, ...otherClasses) { | ||||||
| const deferStack = overrideStackTraceByCode.has(sym); | ||||||
| // Special case for SystemError that formats the error message differently | ||||||
| // The SystemErrors only have SystemError as their base classes. | ||||||
| messages.set(sym, val); | ||||||
| if (def === SystemError) { | ||||||
| def = makeSystemErrorWithCode(sym); | ||||||
| } else { | ||||||
| def = makeNodeErrorWithCode(def, sym); | ||||||
| def = makeNodeErrorWithCode(def, sym, deferStack); | ||||||
| } | ||||||
|
|
||||||
| if (otherClasses.length !== 0) { | ||||||
| otherClasses.forEach((clazz) => { | ||||||
| def[clazz.name] = makeNodeErrorWithCode(clazz, sym); | ||||||
| def[clazz.name] = makeNodeErrorWithCode(clazz, sym, deferStack); | ||||||
| }); | ||||||
| } | ||||||
| codes[sym] = def; | ||||||
|
|
@@ -788,6 +790,40 @@ 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 hideLeadingInternalFrames(error, stackFrames) { | ||||||
| let frames = stackFrames; | ||||||
| if (typeof stackFrames === 'object') { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This came from a line of code in repl written by @devsnek. Wasn't sure if there was some original reason for it. |
||||||
| let beforeUserCode = true; | ||||||
| frames = ArrayPrototypeFilter( | ||||||
| stackFrames, | ||||||
| (frm) => { | ||||||
| if (!beforeUserCode) | ||||||
| return true; | ||||||
| const isInternal = StringPrototypeStartsWith(frm.getFileName(), | ||||||
| 'node:internal'); | ||||||
| if (!isInternal) | ||||||
| beforeUserCode = false; | ||||||
| return !isInternal; | ||||||
| }, | ||||||
| ); | ||||||
|
guybedford marked this conversation as resolved.
Outdated
|
||||||
| } | ||||||
| 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`. | ||||||
|
|
@@ -808,6 +844,7 @@ module.exports = { | |||||
| hideStackFrames, | ||||||
| isErrorStackTraceLimitWritable, | ||||||
| isStackOverflowError, | ||||||
| setArrowMessage, | ||||||
| connResetException, | ||||||
| uvErrmapGet, | ||||||
| uvException, | ||||||
|
|
@@ -842,6 +879,12 @@ module.exports = { | |||||
| // Note: Please try to keep these in alphabetical order | ||||||
| // | ||||||
| // Note: Node.js specific errors must begin with the prefix ERR_ | ||||||
|
|
||||||
| // Custom error stack overrides. | ||||||
| const overrideStackTraceByCode = new SafeMap([ | ||||||
| ['ERR_REQUIRE_ESM', hideLeadingInternalFrames], | ||||||
| ]); | ||||||
|
guybedford marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| 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 +1449,31 @@ 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) { | ||||||
| let msg = `require() of ES Module ${filename}${parentPath ? ` from ${ | ||||||
| parentPath}` : ''} not supported.`; | ||||||
| if (!packageJsonPath) { | ||||||
| if (filename.endsWith('.mjs')) | ||||||
|
guybedford marked this conversation as resolved.
Outdated
|
||||||
| 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 remove "type": "module" from ${packageJsonPath} to ` + | ||||||
| 'treat all .js files as CommonJS (using .mjs for all ES modules ' + | ||||||
| 'instead).\n'; | ||||||
|
guybedford marked this conversation as resolved.
Outdated
|
||||||
| return msg; | ||||||
| }, Error); | ||||||
| E('ERR_SCRIPT_EXECUTION_INTERRUPTED', | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do they have to be lazy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't have to be, but we only hit this path on ERR_REQUIRE_ESM errors. I was following the pattern of other lazy requires used in this module.