TS80007

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

Broken Code ❌

const data = await fetch('https://api.example.com');
tsconfig.json
{
  "compilerOptions": {
    "module": "esnext",
    "target": "ES2020"
  }
}

Fixed Code ✔️

const data = await fetch('https://api.example.com');
 
export {};
tsconfig.json
{
  "compilerOptions": {
    "module": "esnext",
    "target": "ES2020"
  }
}

Alternative:

async function getData() {
  const data = await fetch('https://api.example.com');
  return data;
}
tsconfig.json
{
  "compilerOptions": {
    "module": "esnext",
    "target": "ES2020"
  }
}

Top-level await requires the file to be a module. Add export {} to make it a module, or wrap the code in an async function.