To solve unexpected reserved word await error in JavaScript, declare your function as async. The error “unexpected reserved word await” occurs when we use the ‘await’ keyword inside a not marked as async function. If we need to use the ‘await’, we should make the function async.
Let’s take an example where we get this error.
function getString() { // not marked as async function
// unexpected reserved word 'await'
const str = await Promise.resolve('Hello world!');
return str;
}
// unexpected reserved word 'await'
const res = await Promise.resolve(42);
console.log(res)
Output
file:///Users/krunallathiya/Desktop/Code/R/app.js:3
const str = await Promise.resolve('Hello world!');
^^^^^
SyntaxError: Unexpected reserved word
In this example, we didn’t mark the function as async so that this code will generate an “unexpected reserved word await” error.
To solve this error, declare your function as async.
async function getString() { // not marked as async function
// unexpected reserved word 'await'
const str = await Promise.resolve('Hello world!');
return str;
}
//unexpected reserved word 'await'
const res = await Promise.resolve(42);
console.log(res)
Output
42
This code will run properly with no error. However, if you use Node.js and want to use the await keyword on the top level, then set the type attribute to the module on your package.json file.
See the below package.json file. This is a strict requirement if you use Node.js.
{
"type": "module"
}
Set the attribute in your script tag to use it on the browser.
Use this tag on your HTML file.
<script type="module" src="app.js"></script>
Now you can use the top-level await keyword on your code. For browser:
console.log(await Promise.resolve(20));
That’s it for this tutorial.
Related posts
How to Solve does not provide an export named ‘default’
How to Solve cannot find module and its corresponding type declarations
How to Solve cannot read property of null
How to Solve SyntaxError: Unexpected end of input
How to Solve Uncaught ReferenceError: required is not defined

Rushabh Rupani is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. Rushabh has experience with various programming languages and technologies, including PHP, Python, and expert in JavaScript. He is comfortable working in front-end and back-end development.