Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

SyntaxError: await is only valid in async functions, in a React or Redux file

ErrorSyntaxError: await is only valid in async functions, async generators and modules

Short answer

await can only be used inside an async function or at the top level of a module. The error means the closest enclosing function is not async, or the file is a classic script. Mark that exact function async, or move the awaited call into a separate async function.

  • Applies to Node.js 22, React 19, modern browsers
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the line number in the error and look at the nearest function around that await. That function is the one that needs to be async.

    node --check src/file.js
  2. 2Match what you see, then jump to its fix

    1. 1The error points at an await inside a callback, effect or handlerThe nearest enclosing function is not asyncFix
    2. 2The await is at the top of a CommonJS file, outside any functionTop level await in a file that is not a moduleFix

What does the error look like?

  • SyntaxError: await is only valid in async functions, async generators and modules
  • The error appears for an await inside a callback, effect or Redux thunk

Cause 1 of 2

The nearest enclosing function is not async

1Check

await belongs to the function directly around it. A callback inside an async function is its own function, so the outer async does not cover it.

grep -n "await" src/file.js

2Fix

Add async to the function that contains the await. For an effect, define an async function inside the callback and call it.

useEffect(() => {
  async function load() {
    const res = await fetch('/api/items');
    setItems(await res.json());
  }
  load();
}, []);

3Verify

Parse the file again.

node --check src/file.js

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

Top level await in a file that is not a module

1Check

Top level await is only allowed at the top level of a module. Node.js treats a file as a module when it has the .mjs extension or "type": "module" in package.json; a .cjs file or "type": "commonjs" marks CommonJS.

grep -n "\"type\"" package.json
head -5 src/file.js

2Fix

Make the file a module (an .mjs extension or type module in package.json), or wrap the code in an async function.

async function main() {
  const data = await getData();
  console.log(data);
}
main();

3Verify

Run the file again.

node src/file.js

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Turn on an ESLint or editor check so the error shows while you type.
  • Keep awaited code in small named async functions instead of nested callbacks.
  • Remember that each nested function needs its own async keyword.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.