Top-Level await in Modules
Learn how top-level await works in ES modules on Node.js 24, how it delays importers, where it is not allowed and the patterns where it helps or hurts.

Top-level await lets a module wait for a promise at its own top level, outside any async function. It has been available in ES modules since ES2022, and it works in Node.js when the file is a module, with a .mjs extension or "type": "module" in package.json. The feature is simple to use and has one consequence that deserves attention: a module that awaits delays every module that imports it. This post explains the semantics on Node.js 24 with printed output, shows what ordering you get across imports, lists where it is not available and gives the patterns that work well, such as loading configuration, alongside the ones that make startup slow or fragile.
The basics
In a module, await can appear at the top level, and the module's evaluation pauses until the promise settles.
// config.mjs
const response = await Promise.resolve({ port: 8080 });
export const port = response.port;// main.mjs
import { port } from "./config.mjs";
console.log("port is", port);port is 8080main.mjs does not run until config.mjs has finished evaluating, including its await. The importer sees a fully initialized export. That guarantee is the main benefit: you do not need an exported promise, an init function or a null check for the value.
How it delays importers
A module graph is evaluated depth first, and a module with top-level await becomes an asynchronous module. Its dependents wait for it, and their own evaluation is deferred.
// slow.mjs
console.log("slow: start");
await new Promise((resolve) => setTimeout(resolve, 30));
console.log("slow: done");
export const value = 1;// app.mjs
import { value } from "./slow.mjs";
console.log("app: value is", value);slow: start
slow: done
app: value is 1The app module's body did not start until slow.mjs finished. Importing it with a static import declaration makes the dependency blocking by design. Siblings that do not depend on each other can still evaluate in parallel, because the specification allows independent asynchronous modules to run their awaits at the same time.
// a.mjs
await new Promise((resolve) => setTimeout(resolve, 30));
export const a = "a";// b.mjs
await new Promise((resolve) => setTimeout(resolve, 30));
export const b = "b";// both.mjs
import { a } from "./a.mjs";
import { b } from "./b.mjs";
console.log(a + b);abBoth waits overlap, so the module graph takes about the time of the longest one and not the sum. The difference is real, and it is one reason to keep each top-level wait in its own module and import them as siblings.
Where it is not available
CommonJS.
.jsfiles without"type": "module"and.cjsfiles do not allow top-levelawait. Trying it is a syntax error.Inside a regular function.
awaitstill requiresasync, since only the module body itself gets the exception.Cycles. If two modules import each other, and one of them awaits at the top level, you can end up with a deadlock or with access to uninitialized bindings, depending on who reaches the cycle first.
**
require()of an ES module.** Node.js 22.12 and later canrequire()an ES module synchronously, but if the module graph contains top-levelawait, the call throwsERR_REQUIRE_ASYNC_MODULE, since the load cannot finish synchronously.
// in a CommonJS file
try {
require("./slow.mjs");
} catch (error) {
console.log(error.code);
}ERR_REQUIRE_ASYNC_MODULEIf you publish a package that others may require(), avoid top-level await in it, or export an initialization function, so the package stays usable from both module systems.
Dynamic import is the alternative
A static import with top-level await blocks at startup. A dynamic import() loads on demand and returns a promise, so you decide when to pay.
const mode = "report";
if (mode === "report") {
const { render } = await import("./render.mjs");
console.log(render());
}rendered the reportThis assumes render.mjs exports a render function that returns that string. The module is loaded and evaluated only on the path that needs it, so a command line tool starts fast for the commands that do not. It is also the way to catch a load failure.
try {
await import("./missing.mjs");
} catch (error) {
console.log(error.code);
}ERR_MODULE_NOT_FOUNDGood uses
Configuration and secrets that must exist before anything runs. Reading a file, fetching a remote configuration or decrypting a key at startup, in one module that exports the result.
Feature detection with a fallback. Trying a dynamic import and falling back if it fails.
let sqlite;
try {
sqlite = await import("node:sqlite");
} catch {
sqlite = null;
}
console.log(sqlite ? "built-in sqlite available" : "no built-in sqlite");built-in sqlite availableThe output depends on the Node.js version in use, and on Node.js 24 the first message is the expected one.
Scripts. A one-off script that fetches, processes and prints reads naturally with await at the top and no wrapper function.
Bad uses
Slow network calls in a shared module. If a utility module that fifty others import waits on a remote service, every one of them is delayed, and if the service is down, the whole application fails to start, with an error that points at a deep import and not at the cause. Put such calls inside functions that run on demand, with timeouts and retries.
Work that could run in parallel with startup. A top-level await serializes. If you need the result later and not at import time, start the promise at the top and await it where it is used.
const dataPromise = Promise.resolve({ ready: true }); // started now
export async function getData() {
return dataPromise; // awaited when needed
}Anything that can hang. A top-level await on something that never settles stops the entire importing chain without an error. Node.js exits with code 13 and a warning, "unsettled top-level await", when the event loop empties while a module is still waiting, which at least keeps it from hanging silently, but only when nothing else keeps the process alive.
Testing and tooling
Test runners that load modules through an ES module loader handle top-level await normally. Bundlers need a target that supports it, since some output formats cannot express it, and TypeScript requires module set to es2022, esnext or nodenext and a target of es2017 or higher. If your build fails with "top-level await is not available in the configured target environment", the configuration, and not the code, is the thing to fix.
Deciding whether to use it
A simple test helps. Ask whether the module's exports are meaningless until the awaited value exists. A configuration module, a database client that must connect before use, or a module that loads a WebAssembly binary all pass: nobody can use them early, so waiting at import time removes a whole class of "not ready yet" bugs. A module that offers many unrelated functions and happens to need one remote value for one of them fails the test, and should expose an async function instead, so only the callers that need the value pay for it.
Also consider the failure mode. If the awaited call can fail, decide what the application should do. A rejected top-level await makes the module fail to evaluate, and the import chain above it fails with the same error. For required configuration that is the right outcome, since the program cannot run without it. For optional features, catch the error in the module and export a fallback, so one broken dependency does not stop everything.


