Home > Back-end >  eslint 8 to honor top-level await
eslint 8 to honor top-level await

Time:04-19

i am using top-level await in my nodejs module

You can use the await keyword on its own (outside of an async function) within a JavaScript module.

Files ending with .js are loaded as ES modules when the nearest parent package.json file contains a top-level field "type" with a value of "module"

$ jq -r '.type' package.json
module

$ node --version
v16.14.2

$ npm --version
8.7.0

$ jq -r '.devDependencies.eslint' package.json
8.12.0

when i run eslint, which should have a support for top-level await, i get the error

Parsing error: Cannot use keyword 'await' outside an async function

how do i make eslint honor top-level await?

CodePudding user response:

Make sure that:

  • You are using ESLint >=8.
  • You have the correct parser options: ecmaVersion should be 2022 or "latest", and sourceType should be "module" (ESLint does not respect the "type": "module" setting in package.json files).

Next time you have a question about ESLint, please, show your ESLint configuration (typically .eslintrc.json or .eslintrc.yml) to get more specific advice.

  • Related