SyntaxError: await is only valid in async functions, in a React or Redux file
SyntaxError: await is only valid in async functions, async generators and modulesShort 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.
Fix it fast
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.js2Match what you see, then jump to its fix
What does the error look like?
SyntaxError: await is only valid in async functions, async generators and modulesThe 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.js2Fix
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.jsConfirmed by developer.mozilla.org
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.js2Fix
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.jsConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix