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 parentpackage.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 be2022
or"latest"
, andsourceType
should be"module"
(ESLint does not respect the"type": "module"
setting inpackage.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.