Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ESLint cannot parse async functions

ErrorParsing error: Unexpected token function

Short 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.

  • Applies to ESLint 8, ESLint 9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.js
  2. 2Match what you see, then jump to its fix

    1. 1The config file has no parserOptions.ecmaVersion, or sets a number below 2017 (8)A legacy .eslintrc config leaves ecmaVersion at the defaultFix
    2. 2You 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. 3The ecmaVersion in print-config does not match the file you just editedThe config you edited is not the one that applies to this fileFix

What does the error look like?

  • Parsing error: Unexpected token function
  • The 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 parserOptions

2Fix

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.js

Confirmed by eslint.org

Did this fix it?

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.js

2Fix

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.js

Confirmed by eslint.org

Did this fix it?

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 30

2Fix

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.js

Confirmed by eslint.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

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