---
title: "SyntaxError: await is only valid in async functions and the top level bodies of modules"
description: "This error means you used await somewhere JavaScript cannot pause. Learn why, and fix it by marking the function async or loading the file as an ES module."
url: https://www.danny.engineering/answer/javascript/syntaxerror-await-is-only-valid-in-async-functions-and-the-top-level-bodies-of-modules
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Javascript"]
---

# SyntaxError: await is only valid in async functions and the top level bodies of modules

## The short answer


You used `await` inside a function that is not marked `async`, or at the top of a file that is loaded as a classic script or CommonJS module. Add the `async` keyword to the nearest enclosing function, or load the file as an ES module so top-level `await` is allowed.


```diff
-function loadUser(id) {
+async function loadUser(id) {
   const response = await fetch(`/api/users/${id}`);
   return response.json();
 }
```


## Why it happens


`await` pauses a function until a promise settles. Only two places are allowed to pause like that:

- The body of a function declared with `async` (including async arrow functions and async methods).
- The top level of an ES module, where the module loader can wait before running modules that import it.

Everywhere else, `await` is not a keyword the parser accepts, so the file fails to parse before any code runs. The most common causes are:

- Forgetting `async` on the function, or putting it on the outer function when the `await` is inside an inner callback.
- Using `await` inside a callback passed to `forEach`, `map` or `setTimeout` that is not itself `async`.
- Using top-level `await` in a file that runs as CommonJS in Node.js, or in a browser script tag without `type="module"`.

## How to fix it


### Mark the closest function as async


The `async` keyword must be on the function that directly contains the `await`, not on a function further out.


```javascript
button.addEventListener('click', async () => {
  const data = await fetchData();
  render(data);
});
```


### Fix awaits inside array callbacks


Making a `forEach` callback `async` silences the error but does not wait: `forEach` ignores the promises it gets back. Use a `for...of` loop to run one at a time, or `Promise.all` to run them in parallel.


```javascript
// One after another
for (const id of ids) {
  await saveItem(id);
}

// All at once
await Promise.all(ids.map((id) => saveItem(id)));
```


### Use top-level await in an ES module


In the browser, add `type="module"` to the script tag:


```html
<script type="module" src="/main.js"></script>
```


In Node.js, either name the file with a `.mjs` extension or set `"type": "module"` in the nearest `package.json`:


```json
{
  "type": "module"
}
```


Then this works at the top of the file:


```javascript
const config = await loadConfig();
export default config;
```


### Wrap the code in an async function


If you cannot switch to ES modules, for example in an older CommonJS project, wrap the code in an async function and call it. Handle the rejection so errors are not lost.


```javascript
async function main() {
  const config = await loadConfig();
  startServer(config);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
```


## Common pitfalls

- Adding `async` to the outer function when the `await` sits inside a nested callback. Each function needs its own `async`.
- Making a `forEach` callback async and assuming the loop waits for it. It does not.
- Using top-level `await` in a module that many others import. Every importer waits for it to finish, which can slow start-up.
- Forgetting that the error is a parse error. Nothing in the file runs, so logs before the bad line will not appear either.

## Related reading

- [MDN: await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
- [MDN: async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
- [Node.js: Modules: ECMAScript modules](https://nodejs.org/api/esm.html)
