question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

4.5 nightly nodenext does not allow top-level await (anymore?)

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rbucktoncommented, Oct 19, 2022

I should note, I tested against the following TS versions:

  • typescript@4.5.0-dev.20211101
  • typescript@4.6.0-dev.20211102
  • typescript@4.7
  • typescript@4.8

All supported top-level await correctly when the source file will be treated as a module and not as a CommonJS file.

0reactions
rbucktoncommented, Oct 19, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top-level 'await' expressions are only allowed when the ...
This is a workaround showing the user how to avoid using top level await, rather than an answer. As the error mentions, you...
Read more >
Top-level await in Node.js 14 - YouTube
Top - level await allows you to use the await keyword outside of an async function. Top - level await is available in...
Read more >
Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
TypeScript's New Top-Level Await - Better Programming
We don't need an async function wrapper anymore ... Note: Since Typescript 3.8 it's released and all major browser support top-level await.
Read more >
Top-level await in TS 3.8 Nightly Build? : r/typescript - Reddit
Pretty sure yeah. Before when I was using 3.7 it would give the error saying that await needed to be inside of an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found