standard: Parsing error: Cannot use keyword 'await' outside an async function
What version of this package are you using?
14.3.4
What operating system, Node.js, and npm version?
Node: 14.13.0 npm: 6.14.8 OS: macOS Catalina 10.15.5
What happened?
Given code:
import { fileURLToPath } from 'url'
import fs from 'fs'
const file = await fs.promise.readFile(fileURLToPath(import.meta.url))
console.log(file.toString())
Output is:
standard: Use JavaScript Standard Style (https://standardjs.com)
example.mjs:3:14: Parsing error: Cannot use keyword 'await' outside an async function
What did you expect to happen?
Top-level await is a stage 3 proposal (see https://tc39.es/proposal-top-level-await/) but is enabled by default in from Node 14.8.0 (see https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V14.md#2020-08-11-version-1480-current-codebytere).
This is going to mean that linting with apps written for Node 14+ will be problematic, as top-level await is bound to be used.
Since Node 14 is becoming Active LTS in a few weeks I think TLA should pass linting
Are you willing to submit a pull request to fix this bug?
yes
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 23 (8 by maintainers)
Links to this issue
Commits related to this issue
- Add tests for ensuring top level awaits Fixes https://github.com/standard/standard/issues/1548 — committed to standard/eslint-config-standard by voxpelli 2 years ago
- fix: update eslint-config-standard-jsx fixes #1548 — committed to standard/standard by voxpelli 2 years ago
Sadly it does, but I can confirm that updating the
eslint-config-standard-jsx/eslintrc.jsonto"ecmaVersion": 2022works.With the current code, we can’t upgrade to
eslint@8when it will be released. We’re working on it: https://github.com/standard/standard-engine/issues/234I guess we could release
standard@17witheslint@8to support all the new features, including Top-level await. Also the first v8.0.0 beta prerelease has been published! (see: https://github.com/eslint/eslint/issues/14872 and https://eslint.org/blog/2021/08/eslint-v8.0.0-beta.0-released).17.0.0-2should finally be fixing this + we now have tests that ensures that ensures that’s the case@NemoStein @75lb We just released
17.0.0-1with a fix for this.Unless https://github.com/standard/eslint-config-standard-jsx/issues/43 overrides that fix, then it should work now, else we’ll try to get a fix in for that issue and releases a
17.0.0-2tomorrow.Both me and @Divlo needs some sleep now 😅
Prerelease
17.0.0-0ofstandardis now released, containing ESLint 8 support, which should fix this issue. See: https://github.com/standard/standard/releases/tag/v17.0.0-0Would love if you could all test this and report back here if this is still an issue 🙏 We’ll reopen this if its still an issue.
@willfarrell you can follow Standard 17 (which uses ESLint 8) progress here: https://github.com/standard/eslint-config-standard/issues/208
@extremeheat You can track ESLint progress here: https://github.com/eslint/eslint/issues/14632 It’ll be released in ESLint 8.0. Once ESLint is updated, Standard surely will follow shortly after.
Regarding Node.js support, it has been supported prior to 14.8.0, but just behind a command line flag. Even with it unflagged, Node.js still documents the support has experimental, of course (https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_experimental_top_level_await). I’m not sure why it is still marked as experimental unless they are waiting for it to enter either Stage 4 or it become part of the standard.
Typically standard doesn’t enable this type of support until it is in stage 4 (https://github.com/standard/standard#how-do-i-use-experimental-javascript-es-next-features).
@NemoStein Yeah, I arrived at the same conclusion, will merge and release https://github.com/standard/eslint-config-standard/pull/212 when another maintainer gives me a review 👍
@NemoStein Right, thanks, I’ll reopen. Have you tested in plain ESLint 8 and seen if it works there? Maybe we need to explicitly allow it?
In regards to the VSCode extension, that one needs an update, I highlighted that in https://github.com/standard/eslint-config-standard/issues/208 now 🙏
@voxpelli Something wrong is not right
Furthermore, VSCode (
standard.vscode-standard v2.0.1) don’t show Standard errors anymore.Hopefully this is happening soon! 🎉
Top level await has been stage 4 for a while, is there any fix?
@Ethan-Arrowood I’m ignoring the files containing top-level
awaitfor now:Thanks for the suggestion @davidmarkclements and for the helpful discussion @dougwilson. Unfortunately, there’s not much we can do to easily enable support for this. The language parser used by ESLint has a policy of not supporting features until Stage 4.
So this is not solvable unless we decide to start shipping the
babel-eslintparser instead of the default ESLint one, which is a much larger decision to make.This issue will be fixed once the feature enters Stage 4.
To clarify, I am not a member of this project and cannot make decisions etc. I was just trying to help by connecting your question to the project’s written policy.