Short answer
Find the function that directly contains the await and add async to it. If the await is inside a callback such as forEach or map, that callback is the function that needs async, and forEach will not wait for it, so use a for...of loop or Promise.all instead. For await at the top level, run the file as an ES module: type="module" on the script tag in browsers, or .mjs or "type": "module" in package.json for Node.js.
Where the await is, and the fix
01
A function that is not marked async
“Therefore, await cannot be used in sync tasks, such as functions, generator functions, or top level of scripts.” MDN, SyntaxError: await is only valid in async functions
await is only allowed directly inside an async function. Add async to the function that contains it, then await that function where it is called.
// wrong function loadUser(id) { const res = await fetch(`/api/users/${id}`); } // right async function loadUser(id) { const res = await fetch(`/api/users/${id}`); return res.json(); }02
Inside a forEach, map or event callback
The outer function may be async, but the arrow function passed to forEach is a separate function. Marking that callback async removes the error, but forEach still does not wait. Use for...of to run in order, or Promise.all with map to run in parallel.
for (const id of ids) { await save(id); } // or in parallel await Promise.all(ids.map((id) => save(id)));03
At the top level of a script
“await at the top level of a module.” Node.js, Packages: syntax detection
Top-level await works only in ES modules. In the browser, a classic script tag is not a module. In Node.js, .cjs files and packages with "type": "commonjs" are CommonJS. Node.js 22.7 and 20.19 and newer treat a .js file with no "type" field in the nearest package.json as a module when it uses top-level await, so the error mostly appears in older versions and in explicit CommonJS. If that file also calls require(), these versions throw a different error instead (Cannot determine intended module format): switch to import, or wrap the await in an async function.
<script type="module" src="/main.js"></script> // package.json { "type": "module" }04
When the file has to stay CommonJS
Wrap the top-level code in an async function and call it, handling errors so a rejection does not go unnoticed.
async function main() { const config = await loadConfig(); startServer(config); } main().catch((err) => { console.error(err); process.exit(1); });
Checked against the vendor’s docs on : MDN: SyntaxError: await is only valid in async functions; Node.js: Packages (syntax detection).
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.See scope and start
Related: Node.js “Cannot use import statement outside a module” · “Uncaught (in promise)” · Node.js ERR_REQUIRE_ESM