Skip to content
Latchkey

esbuild "Top-level await is currently not supported" in CI

Your code uses top-level await, but esbuild is emitting a format (CJS or IIFE) or a target that cannot represent it. TLA requires an ESM output and a recent target.

What this error means

The build fails with "Top-level await is currently not supported with the cjs output format". The source has an await at module scope that the chosen output cannot encode.

node
> ERROR: Top-level await is currently not supported with the "cjs" output format

Common causes

Output format is CJS or IIFE

Top-level await only exists in ES modules. CJS and IIFE outputs have no way to express it.

Target is too old

A target like es2020 predates standardized TLA, so esbuild refuses to emit it.

How to fix it

Emit ESM with a TLA-capable target

Switch the output format to esm and raise the target to one that supports top-level await.

build script
esbuild.build({ format: 'esm', target: 'es2022' });

Remove top-level await

If the output must stay CJS, wrap the awaited work in an async function instead of awaiting at module scope.

source
async function main() {
  const data = await load();
}
main();

How to prevent it

  • Use ESM output and an es2022+ target when relying on top-level await.
  • Keep async initialization inside an async function for CJS outputs.
  • Match esbuild target to the runtime that will execute the bundle.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →