i am using top-level await in my nodejs module
You can use the
awaitkeyword on its own (outside of an async function) within a JavaScript module.
Files ending with
.jsare loaded as ES modules when the nearest parentpackage.jsonfile 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:
ecmaVersionshould be2022or"latest", andsourceTypeshould be"module"(ESLint does not respect the"type": "module"setting inpackage.jsonfiles).
Next time you have a question about ESLint, please, show your ESLint configuration (typically .eslintrc.json or .eslintrc.yml) to get more specific advice.
