You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Top-level await error message is extremely unclear #36036
'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher.
This is really unclear! My compiler options are all set correctly, so why is this not working?
It's because it's not a module. This error message is expecting me to meet 3 different conditions, "top level of a module" is unclear for any user who just wants to use this feature. They'll likely take "module" to just mean "file".
Reacted by Karel Kral, Csaba, Noam Bendelac, Dimitri Leurs, Joe Lust, Elliott G Dehnbostel, btoo, Stéphane Bruckert, William Derksen, Ed Graham and 6 more
When it occurs at the top level of a file (not a regular function), regardless of compiler options:
`await` expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
and when the options are wrong:
Top level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
When in a regular function
'await' expressions are only allowed within async functions and at the top levels of modules.
Personally, I'd prefer just one at a time, but both are fine at once.
I'm generally not a fan of fixing one error just to end up with another error when the fix had nothing to do with the second error. I'd rather get both so I know all the actions I need to take.
Reacted by hlovdal, Henrique Nepo, William Derksen, Cody Swartz, Sean Duncan, Keith Harvey, NERDDISCO and Bryan Grounds
Actually, it looks like we have. We have fixes already for enabling jsx (services/codefixes/fixEnableJsxFlag.ts) and experimentalDecorators (services/codefixes/fixEnableExperimentalDecorators.ts).
Daniel Rosenwasser (@DanielRosenwasser): Having a codefix to change your module to system when its currently set to commonjs seems possibly bad as those module systems are incompatible. Should we still have the quick fix, or should we only have one for module: es2015 -> module: esnext?
For 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.:
Add 'export {}' to make this file into a module.
For Top level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.:
Set the 'module' option in your configuration file to '{0}', and the 'target' option to '{1}' (if both are invalid and fixable)
Set the 'module' option in your configuration file to '{0}' (if module or both are invalid and fixable)
Set the 'target' option in your configuration file to '{0}' (if target or both are invalid and fixable).
I like being able to do both in one action, but Set the 'module' option in your configuration file to '{0}', and the 'target' option to '{1}' seems very wordy. I'm tempted to use a message like this instead:
tsconfig.json: Set 'module' to '{0}' and 'target' to '{1}'
We would be able to fix the "target" as "es2017", but we can't fix the "module" (which is "commonjs" by default here). Should we even offer a codefix for "target" if we won't be able to fix "module" later?
After struggling with error error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher. for long time, I found this thread. I solved my problem by adding export {}; to my file. I do use ES6 modules, but this particular file does not need any exports.
Please adjust the error message to provide a hint, that apart from adjusting the module and target parameters, the concrete file needs an export {}; line.
Also think having a export {} is unnecessary. should just remove that warning all together i think.
just having a top level await should assume that it's of type=module
top level await should act the same way as having export {} in the code.
More and more ppl are starting to use ESM, so your assumption that ppl use cjs or script by default is, Meh.
Activity
rbuckton commentedon Jan 6, 2020
Yeah, this is a tough one to write concisely. Do you have a better suggestion?
DanielRosenwasser commentedon Jan 6, 2020
When it occurs at the top level of a file (not a regular function), regardless of compiler options:
and when the options are wrong:
When in a regular function
rbuckton commentedon Jan 6, 2020
Would you report both of the top two errors if both are a problem?
rbuckton commentedon Jan 6, 2020
Should we have a codefix for the first one to add an
export {}to the file?Also, have we considered adding codefixes to fix the
tsconfig.jsonfor things like the 2nd error message?DanielRosenwasser commentedon Jan 6, 2020
Personally, I'd prefer just one at a time, but both are fine at once.
Always!
I think we do, but check with Sheetal Nandi (@sheetalkamat)
rbuckton commentedon Jan 6, 2020
I'm generally not a fan of fixing one error just to end up with another error when the fix had nothing to do with the second error. I'd rather get both so I know all the actions I need to take.
sheetalkamat commentedon Jan 6, 2020
No we haven't done that yet and there is #30739 that needs something similar...
rbuckton commentedon Jan 6, 2020
Actually, it looks like we have. We have fixes already for enabling
jsx(services/codefixes/fixEnableJsxFlag.ts) andexperimentalDecorators(services/codefixes/fixEnableExperimentalDecorators.ts).rbuckton commentedon Jan 6, 2020
Daniel Rosenwasser (@DanielRosenwasser): Having a codefix to change your
moduletosystemwhen its currently set tocommonjsseems possibly bad as those module systems are incompatible. Should we still have the quick fix, or should we only have one formodule: es2015->module: esnext?DanielRosenwasser commentedon Jan 6, 2020
es2015toesnext, especially sincemoduleResolutioncan change when changing fromcommonjsrbuckton commentedon Jan 7, 2020
What do you think of these code fix names?
For
'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.:Add 'export {}' to make this file into a module.For
Top level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.:Set the 'module' option in your configuration file to '{0}', and the 'target' option to '{1}'(if both are invalid and fixable)Set the 'module' option in your configuration file to '{0}'(ifmoduleor both are invalid and fixable)Set the 'target' option in your configuration file to '{0}'(iftargetor both are invalid and fixable).I like being able to do both in one action, but
Set the 'module' option in your configuration file to '{0}', and the 'target' option to '{1}'seems very wordy. I'm tempted to use a message like this instead:rbuckton commentedon Jan 13, 2020
Daniel Rosenwasser (@DanielRosenwasser):
Consider the following:
We would be able to fix the
"target"as"es2017", but we can't fix the"module"(which is"commonjs"by default here). Should we even offer a codefix for"target"if we won't be able to fix"module"later?DanielRosenwasser commentedon Jan 13, 2020
I think it's fine to say "no, we can't give a quick fix in all cases", and that might be one of them
dilyanpalauzov commentedon Dec 4, 2021
After struggling with error
error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.for long time, I found this thread. I solved my problem by addingexport {};to my file. I do use ES6 modules, but this particular file does not need any exports.Please adjust the error message to provide a hint, that apart from adjusting the
moduleandtargetparameters, the concrete file needs anexport {};line.serg06 commentedon Feb 24, 2022
It fixed for me when I changed
"module": "ESNext"to"module": "es2022".Qix- commentedon Feb 27, 2022
Yeah serg06 is right here. Why do
ESNextandnodenextnot work? Aggravating.avin-kavish commentedon Aug 10, 2022
Why do we still need to add
export {}to mark it as a module, why can't it implicitly be one whentype: "module"is set?DanielRosenwasser commentedon Aug 10, 2022
moduledoesn't always imply that you don't have any globals in your program.If that's not the behavior you want, in newer versions of TypeScript, you can configure this with the
moduleDetectionoption and set it toforce.jimmywarting commentedon May 15, 2023
Also think having a
export {}is unnecessary. should just remove that warning all together i think.just having a top level await should assume that it's of type=module
top level await should act the same way as having
export {}in the code.More and more ppl are starting to use ESM, so your assumption that ppl use cjs or script by default is, Meh.