-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
esm: refine ERR_REQUIRE_ESM errors #39175
Conversation
@GeoffreyBooth I know we went through this many times before, but since it keeps coming up I'm happy to go through the same discussions again on the exact wording. |
ff5d5f7
to
1f59c8f
Compare
lib/internal/errors.js
Outdated
// Hide stack lines before the first user code line. | ||
function hideLeadingInternalFrames(error, stackFrames) { | ||
let frames = stackFrames; | ||
if (typeof stackFrames === 'object') { |
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.
nit: %Array.isArray%
would be more readable (or maybe stackFrames !== undefined
would be enough?).
if (typeof stackFrames === 'object') { | |
if (ArrayIsArray(stackFrames)) { |
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.
This came from a line of code in repl written by @devsnek. Wasn't sure if there was some original reason for it.
d41671c
to
8be551d
Compare
now that my pr landed, you can remove |
@devsnek working great - I've pushed that up. |
Ping @nodejs/modules for further reviewers on the error message improvements here. |
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.
Seems fine to me, there are cheaper top level parsers, but since we are on an error path, this seems fine.
@@ -788,6 +789,34 @@ const fatalExceptionStackEnhancers = { | |||
} | |||
}; | |||
|
|||
// Ensures the printed error line is from user code. | |||
let _kArrowMessagePrivateSymbol, _setHiddenValue; |
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.
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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>
Landed in e2a6399. |
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>
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>
since node 16.6 webpack-cli fails to load esm config unless WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG=true see: nodejs/node#39175 see: https://github.com/webpack/webpack-cli/blob/a660ffcbeb2807bce1554f787297e697464abd59/packages/webpack-cli/lib/webpack-cli.js#L50-L51 License: (Apache-2.0 AND MIT) Signed-off-by: Oli Evans <oli@tableflip.io>
Since node v16.6 webpack-cli fails to load esm config unless WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG=true see: nodejs/node#39175 see: https://github.com/webpack/webpack-cli/blob/a660ffcbeb2807bce1554f787297e697464abd59/packages/webpack-cli/lib/webpack-cli.js#L50-L51 License: (Apache-2.0 AND MIT) Signed-off-by: Oli Evans <oli@tableflip.io>
Since node v16.6 webpack-cli fails to load esm config unless WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG=true see: nodejs/node#39175 see: https://github.com/webpack/webpack-cli/blob/a660ffcbeb2807bce1554f787297e697464abd59/packages/webpack-cli/lib/webpack-cli.js#L50-L51 License: (Apache-2.0 AND MIT) Signed-off-by: Oli Evans <oli@tableflip.io>
In nodejs#39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error.
In nodejs#39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: nodejs#39175
In #39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: #39175 PR-URL: #49521 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
In #39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: #39175 PR-URL: #49521 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
In nodejs#39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: nodejs#39175 PR-URL: nodejs#49521 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
In nodejs#39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: nodejs#39175 PR-URL: nodejs#49521 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
In #39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: #39175 PR-URL: #49521 Backport-PR-URL: #50669 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
In #39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: nodejs/node#39175 PR-URL: nodejs/node#49521 Backport-PR-URL: nodejs/node#50669 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
In #39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error. Refs: nodejs/node#39175 PR-URL: nodejs/node#49521 Backport-PR-URL: nodejs/node#50669 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This PR attempts to improve the usability of the ERR_REQUIRE_ESM error messages with two major improvements:
"Instead change the require of X.js in Y.js to a dynamic import() which is available in all CommonJS modules."
which avoids confusion since we currently suggest all three of that plus changing the type and the extension to cjs, both of which are not appropriate in this case. This is implemented by using the acorn parser on the source at error time.arrow_message_private_symbol
to JS so that JS code can inject this frame. The code to reconstruct the frame is more custom than it should be - ideally this could use the standard stack trace API with full source maps integration and be customizable in a way that fits with the stack trace overrides. I hope we can continue to improve this primitives over time to get better error messages more easily in Node.js.In addition the internal frames are stripped from the error stack to remove the redundant internal stack lines to ensure that the output is as simple as possible for this error message.
I've provided two examples below. One for requiring CJS that Node.js thinks is ESM, and one requiring ESM that actually contains import / export syntax.
Requiring ESM with ESM syntax Before this PR:
Requiring ESM with ESM syntax after this PR:
Requiring a .js file with no import or export syntax before this PR:
Requiring a .js file with no import or export syntax after this PR:
Requiring a .mjs file before this PR:
Requiring a .mjs file after this PR:
@nodejs/modules