4.5 nightly nodenext does not allow top-level await (anymore?)
See original GitHub issueBug Report
I just switched to 4.5 nightly due to the change in the release of 4.5 with node12 and nodenext and tried to work on my esm branch to get things running again, but somehow 4.5 nightly ( “typescript”: “^4.6.0-dev.20211119”) does not allow top-level await anymore, even though it says:
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.
my ts config has
"module": "nodenext",
"target": "es2021",
it also does not work with node12 (obviously) or other target settings. It does work with module esnext though
🔎 Search Terms
nodenext, top-level await
🕗 Version & Regression Information
I’m not 100% sure, but I believe it worked in the ts4.5 alpha already.
- This is a crash
🙁 Actual behavior
cannot compile something like this
// @hokify/schedular is an ESM module
const { AgendaScheduler } = await import('@hokify/scheduler');
in top-level code.
🙂 Expected behavior
should compile
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6 (6 by maintainers)
Top GitHub Comments
I should note, I tested against the following TS versions:
All supported top-level
await
correctly when the source file will be treated as a module and not as a CommonJS file.@simllll: I cannot repro this. Is your project configured correctly? Top-level
await
is only supported in--module nodenext
when the input file is an ES module, not a CommonJS module. I tested this with both an.mts
source file and with a.ts
file in a folder with a package.json containing{ "type": "module" }
.In 4.7 we changed the error message to
error TS1309: The current file is a CommonJS module and cannot use 'await' at the top level.
to clarify what’s wrong.