ESLint cannot parse async functions
Parsing error: Unexpected token functionShort answer
async and await are ES2017 syntax. A legacy .eslintrc file expects ECMAScript 5 unless you raise parserOptions.ecmaVersion, or enable an env such as es2017. Raise it to 2017 or higher, or use latest. In a flat config, ecmaVersion lives under languageOptions and defaults to latest.
Fix it fast
1Find out which cause you have
Print the configuration ESLint applies to the failing file. It shows the real ecmaVersion after every extends and override has been merged.
npx eslint --print-config src/index.js2Match what you see, then jump to its fix
- 1
The config file has no parserOptions.ecmaVersion, or sets a number below 2017 (8)A legacy .eslintrc config leaves ecmaVersion at the defaultFix - 2
You use eslint.config.js, yet async code still fails to parseA flat config sets a low ecmaVersion, or puts it in the wrong placeFix - 3
The ecmaVersion in print-config does not match the file you just editedThe config you edited is not the one that applies to this fileFix
- 1
What does the error look like?
Parsing error: Unexpected token functionThe error points at the line where an async function starts
Cause 1 of 3
A legacy .eslintrc config leaves ecmaVersion at the default
1Check
The ESLint 8 docs state that by default ESLint expects ECMAScript 5 syntax. Setting the env es2017 or es6 also sets ecmaVersion, to 8 or 6. Check which number your print-config output shows.
npx eslint --print-config src/index.js | grep -A4 parserOptions2Fix
Set parserOptions.ecmaVersion to 2017 or newer, or to latest. An env such as es2017 raises it too, and adds the globals of that year.
// .eslintrc.json
{
"parserOptions": {
"ecmaVersion": "latest"
}
}3Verify
Run ESLint on the file that failed. The parsing error is gone.
npx eslint src/index.jsConfirmed by eslint.org
Cause 2 of 3
A flat config sets a low ecmaVersion, or puts it in the wrong place
1Check
In flat config, ecmaVersion defaults to latest. The docs advise setting ecmaVersion and sourceType directly under languageOptions, not inside languageOptions.parserOptions. Check every config object whose files pattern matches the file.
grep -n -B3 -A3 ecmaVersion eslint.config.js2Fix
Delete the low value, or set it under languageOptions.
// eslint.config.js
export default [
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
},
];3Verify
The print-config output shows ecmaVersion latest for the file, and the lint run passes the parser.
npx eslint --print-config src/index.js | grep ecmaVersion
npx eslint src/index.jsConfirmed by eslint.org
Cause 3 of 3
The config you edited is not the one that applies to this file
1Check
A config object without a files key applies to any file that another object matches. Objects with files patterns only apply to matching files, and by default ESLint lints .js, .cjs and .mjs. A file type such as .ts needs its own object.
npx eslint --print-config src/index.js | head -n 302Fix
Put the language options in an object whose files pattern covers the failing file.
export default [
{
files: ["**/*.{js,mjs,cjs}"],
languageOptions: { ecmaVersion: "latest" },
},
];3Verify
Run ESLint on the file again.
npx eslint src/index.jsConfirmed by eslint.org
How do I stop it coming back?
- Set ecmaVersion once, to latest, instead of a fixed year, so new syntax never breaks the parser.
- Run npx eslint --print-config on one file of each type whenever you change the config.
- Move to flat config (eslint.config.js), where latest is the default.
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
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix