Fixing TS1378 “Top-level ‘await’ expressions are only allowed” in TypeScript
error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to ... is reported by tsc when the module or target in tsconfig.json does not support top-level await (an await written outside any function). If you run the code with Node.js, change "module" to "nodenext". If you pass the code to a bundler such as Vite, change it to "module": "esnext". Also set target to es2017 or higher, and the error goes away.
In a Node.js script, you may want to load configuration or wait for something at the top of the file with await, without wrapping it in an async function. If you type-check that code with a tsconfig.json whose module is commonjs, you get this error. I reproduced the situation and tried the fixes.
The test environment was Ubuntu 24.04, Node.js 22.23.5, npm 10.9.9, and TypeScript 5.9.3. I also ran the same steps with TypeScript 7.0.2, which was the npm latest version on the day I tested. I only checked the output of tsc and node. I did not test TypeScript 6.x, editor displays such as VS Code, builds with bundlers such as Vite, or Deno and Bun.
- 1. With module set to commonjs, the error occurs
- 2. The result depends on the combination of module and target
- 3. To run with Node.js, set module to nodenext
- 4. To pass the code to a bundler, set module to esnext
- 5. Removing export {} causes TS1375
- 6. The error also appears without a module setting
- 7. Results with TypeScript 7.0.2
With module set to commonjs, the error occurs
I created an empty folder, put a package.json with "type": "module" in it, and installed TypeScript 5.9.3. The tsconfig.json was as follows.
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"strict": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
src/main.ts waits 100 milliseconds and then prints a message. The await on line 4 is not inside any function. The meaning of the final export {}; is explained in a later section.
const wait = (ms: number) =>
new Promise<string>((resolve) => setTimeout(() => resolve(`${ms}ms 待ちました`), ms));
const message = await wait(100);
console.log(message);
export {};
Running npx tsc produced the following error, and the exit code was 2.
$ npx tsc
src/main.ts(4,17): 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.
The message means: "Top-level await can be used only when module is one of es2022, esnext, system, node16, node18, node20, nodenext, or preserve, and target is es2017 or higher." Two conditions, module and target, must both be met. With --pretty, a ~ mark appeared under the await (colors omitted).
$ npx tsc --pretty
src/main.ts:4:17 - 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.
4 const message = await wait(100);
~~~~~
Found 1 error in src/main.ts:4
Even with the error, tsc still wrote dist/main.js. Its contents were in CommonJS form, and the top-level await was left as it was.
$ cat dist/main.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const wait = (ms) => new Promise((resolve) => setTimeout(() => resolve(`${ms}ms 待ちました`), ms));
const message = await wait(100);
console.log(message);
Running node dist/main.js stopped Node.js, which could not decide whether to read the file as CommonJS or as an ES module. The exit code was 1. (The path is replaced with /path/to/app, and the stack trace and the Node.js version line at the end are omitted.)
$ node dist/main.js
file:///path/to/app/dist/main.js:2
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: Cannot determine intended module format because both require() and top-level await are present. If the code is intended to be CommonJS, wrap await in an async function. If the code is intended to be an ES module, replace require() with import.
...
code: 'ERR_AMBIGUOUS_MODULE_SYNTAX'
}
TS1378 reports during type checking that the code uses top-level await, which can only be written in an ES module, while the output is configured as CommonJS.
The result depends on the combination of module and target
I kept --target es2022 and changed only --module, then checked with npx tsc --noEmit against the same src/main.ts. The results for 5.9.3 were as follows.
module value |
Result |
|---|---|
commonjs, es2015, es2020, amd, umd, none |
TS1378 |
es2022, esnext, preserve |
No error |
node16, node18, node20, nodenext |
No error |
system |
No error |
Even es2020, which supports import.meta, is not enough for top-level await. The TypeScript modules reference also states that top-level await is supported starting with es2022.
Next I kept --module esnext and changed only --target.
target value |
Result |
|---|---|
es5, es2015, es2016 |
TS1378 |
es2017, es2020, es2022 |
No error |
With es5, TS2585 about Promise also appeared. Even with the correct module, TS1378 does not go away if target is es2016 or lower. A tsconfig.json with "target": "es2016" and "module": "esnext" produced TS1378 with the same message. This is the case where the second half of the message, "target is es2017 or higher," is not met.
To run with Node.js, set module to nodenext
I changed "module": "commonjs" in the first tsconfig.json to "module": "nodenext". I kept target at es2022 and left package.json unchanged. The tsconfig reference for module also says that for a current Node.js project, nodenext is usually the right choice.
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
npx tsc printed nothing and exited with code 0, and TS1378 was gone. The output dist/main.js was in ES module form, and node dist/main.js waited 100 milliseconds and then printed the message.
$ cat dist/main.js
const wait = (ms) => new Promise((resolve) => setTimeout(() => resolve(`${ms}ms 待ちました`), ms));
const message = await wait(100);
console.log(message);
export {};
$ node dist/main.js
100ms 待ちました
Without type: module, the error changes to TS1309
With "module": "nodenext" still set, I removed "type": "module" from package.json. Instead of TS1378, a different error appeared. The exit code was 2.
$ npx tsc
src/main.ts(4,17): error TS1309: The current file is a CommonJS module and cannot use 'await' at the top level.
The message means: "This file is a CommonJS module, so await cannot be used at the top level." According to the modules reference section on module format detection, with nodenext a .ts file is treated as either an ES module or a CommonJS module based on the "type" in the nearest package.json above it. Without "type": "module", it is CommonJS.
To fix this, either add "type": "module" back to package.json or rename the file to src/main.mts. After renaming to .mts, npx tsc exited with code 0, dist/main.mjs was output, and node dist/main.mjs printed 100ms 待ちました.
To pass the code to a bundler, set module to esnext
When a bundler such as Vite produces the JavaScript and tsc is used only for type checking, I used the following tsconfig.json.
{
"compilerOptions": {
"target": "es2022",
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
"noEmit": true
},
"include": ["src"]
}
npx tsc exited with code 0, and TS1378 did not appear. Because of noEmit, no dist folder was created. The tsconfig reference for module also recommends preserve or esnext for code that is passed to a bundler.
Removing export {} causes TS1375
The final export {}; in src/main.ts makes the file be treated as a module. According to the TypeScript 3.8 release notes, top-level await section, top-level await is allowed only inside modules, and TypeScript treats a file as a module only if it has an import or export.
When I removed export {}; and type-checked the file with the bundler tsconfig.json above, TS1375 appeared. The exit code was 2.
$ npx tsc
src/main.ts(4,17): error TS1375: '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.
As the message suggests, adding export {}; made the error go away. If the file already has another import or export, you do not need to add it.
On the other hand, with a nodenext tsconfig.json and "type": "module" in package.json, there was no error without export {};. According to the tsconfig reference for moduleDetection, the default "auto" setting also checks whether package.json's "type" is "module" when module is node16 or nodenext, and uses that to decide whether a file is a module.
The error also appears without a module setting
When I removed only the "module": "commonjs" line from the first tsconfig.json, npx tsc in 5.9.3 still produced TS1378. Checking the values actually used with npx tsc --showConfig showed that module was es6.
$ npx tsc --showConfig | grep -E "\"(target|module|moduleResolution)\""
"target": "es2022",
"module": "es6",
"moduleResolution": "classic",
The module section of the tsconfig reference says the default is commonjs when target is es5, and es6 (es2015) otherwise. The 5.9.3 result matched that. Neither of these supports top-level await. In this case, the output dist/main.js was in ES module form, and node dist/main.js printed 100ms 待ちました. The error is reported even though the code runs, so it is safest to set module explicitly and pass the type check.
Results with TypeScript 7.0.2
With TypeScript 7.0.2, the tsconfig.json using "module": "commonjs" also produced TS1378. The message was identical to 5.9.3, and the exit code was 2. Changing to nodenext gave exit code 0, and node dist/main.js printed 100ms 待ちました. TS1309 when "type": "module" was removed, and TS1375 when export {}; was removed, also appeared with the same text as 5.9.3. However, with a no-emit setting (noEmit), the exit code was 1 in 7.0.2, not 2 as in 5.9.3.
There were two differences. The first is that fewer values are accepted. In 7.0.2, specifying system, amd, or umd for --module, or es5 for --target, produced TS5108 (removed option). With system, amd, and umd, TS5095 also appeared. --module none produced TS6046 (value not allowed). These values were deprecated in the TypeScript 6.0 release notes, which announced they would be removed in 7.0. The error message still lists 'system', but 7.0.2 does not accept system as a choice.
The second difference concerns a tsconfig.json with no module setting. In 7.0.2, the same tsconfig.json produced no error, and the output dist/main.js was in ES module form. The default value changes in the 6.0 release notes say that the default module changed to esnext. With this tsconfig.json, simply upgrading to TypeScript 7.0.2 made TS1378 disappear.




