Uh oh!
There was an error while loading. Please reload this page.
module: Improves Top-Level await error in cjs - #35196
Conversation
Top-Level await is support in esm and not in cjs. The default error message is confusing.
nodejs-github-bot
commented
Sep 14, 2020
Review requested:
|
| lib/punycode.js | ||
| test/addons/??_* | ||
| test/fixtures | ||
| test/message/cjs_top_await_error.js |
There was a problem hiding this comment.
This is here because eslint doesn't play well with top-level await yet.
MylesBorins
commented
Sep 14, 2020
Thanks @bdougie! CI is locked down right now for a security release, but we'll get the full suite kicked off once we can. |
devsnek
commented
Sep 14, 2020
this almost seems like it would be better to do upstream. |
bdougie
commented
Sep 14, 2020
@devsnek, can you expand on what you mean by upstream? Is there a better place for the message? |
devsnek
commented
Sep 14, 2020
I mean changing this in V8 itself, instead of modifying the message in node. |
mscdex
commented
Sep 15, 2020
I agree changing this in V8 would be a better solution than having to maintain a error message string check like this. |
We already have done this once, catching and editing the message in the repl. Improving this error in V8 is absolutely something we can do but it will likely have to wait until V8 itself unflags Top-Level Await. In the mean time this seems like a significant improvement to the status quo with very little overhead prior art: https://github.com/nodejs/node/blob/master/lib/repl.js#L582-L591 |
MylesBorins
commented
Sep 15, 2020
It also worth mentioning that the replaced error message is specifically referencing common.js, a unique goal from the script goal. V8 would not land a message specific to the node runtime |
| } catch (err) { | ||
| if (process.mainModule === cjsModuleInstance) | ||
| enrichCJSError(err); | ||
| if (err.message.includes('await is only valid in async function')) { |
There was a problem hiding this comment.
what happens if you plug in this code:
functionx(){await1;}There was a problem hiding this comment.
/Users/mylesborins/code/node/main/lol.js:2await1;^^^^^
SyntaxError: Top-LevelawaitisonlysupportedinESM.atwrapSafe(internal/modules/cjs/loader.js:1005:16)atModule._compile(internal/modules/cjs/loader.js:1058:27)atObject.Module._extensions..js(internal/modules/cjs/loader.js:1115:10)atModule.load(internal/modules/cjs/loader.js:954:32)atFunction.Module._load(internal/modules/cjs/loader.js:795:14)atFunction.executeUserEntryPoint[asrunMain](internal/modules/run_main.js:72:12)atinternal/main/run_main_module.js:17:47Yeah, this is not good. Do you think there is a way to distinguish this without having to parse the entire file?
targos
commented
Sep 15, 2020
Top-level await is not specific to the node runtime, is it? |
devsnek
commented
Sep 23, 2020
landed above as 4263f8a5e8e04a766aeb7cde0081da3ac6c12a9e in v8 |
MylesBorins
commented
Oct 14, 2020
Closing in lieu of #35650 |
Top-Level await is supported in ESM and not in cjs. The default error
the message is confusing. I provided a clearer one and opened this PR with a lot of help from @MylesBorins.
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes