Top-level 'await' expressions are only allowed when the 'module' option is se...
Production Risk
Build will fail; resolve before shipping.
A syntax error (TS1378): Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.. This diagnostic is emitted by the TypeScript compiler when top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher..
- 1await used outside an async function, or async/await return type mismatch
TypeScript compiler reports TS1378 during type checking.
// Triggers TS1378 // Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
expected output
error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
Fix
Add 'async' to the containing function
WHEN Using await outside an async function
// Mark the function as async
async function fetchData() {
const result = await fetch('/api/data');
return result.json();
}Why this works
await can only be used inside async functions; the 'async' keyword makes the function return a Promise.
TypeScript Compiler Diagnostics
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev