SyntaxError: await is only valid in async functions and the top level bodies of modules
await was used inside a function that isn’t async (or in a classic, non-module script).
Seen on:
JavaScript
Meaning
Callbacks (forEach, event listeners, array methods) are separate functions — they must themselves be async.
Common causes
- Missing async on the enclosing function
- await inside forEach/map callback
- Top-level await in a non-module script or CommonJS
⚡ Quick fix
- Mark the function async
- Use for…of instead of forEach for sequential awaits
- Use type="module" or "type": "module" for top-level await
Detailed fix by platform
JavaScript
- javascript
for (const id of ids) { await save(id); }
How to diagnose
- Function — Which function contains the await?
- Context — Module or classic script?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026