Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
davidmarkclements opened this issue Oct 7, 2020 · 23 comments · Fixed by standard/eslint-config-standard#212 or #1775 · May be fixed by gopuff/azurevmjob#2
Closed
Assignees
Labels
Milestone

Comments

@davidmarkclements
Copy link

@davidmarkclements davidmarkclements commented Oct 7, 2020

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

@dougwilson
Copy link

@dougwilson dougwilson commented Oct 7, 2020

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

@dougwilson
Copy link

@dougwilson dougwilson commented Oct 7, 2020

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.

@davidmarkclements
Copy link
Author

@davidmarkclements davidmarkclements commented Oct 18, 2020

Thanks @dougwilson - this is fair enough and it's one of the downsides to not putting experimental features behind flags.

My only counterpoint here is for the sake of pragmatism: if it's not behind a flag, people will use it without checking whether it's experimental and while this isn't in any way the linters fault, the linter may be blamed regardless.

@dougwilson
Copy link

@dougwilson dougwilson commented Oct 18, 2020

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.

@feross
Copy link
Member

@feross feross commented Oct 21, 2020

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-eslint parser 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.

@feross feross added the blocked label Oct 21, 2020
@davidmarkclements
Copy link
Author

@davidmarkclements davidmarkclements commented Oct 22, 2020

Thanks @dougwilson and thanks @feross for a useful discussion ✌️

@feross feross closed this as completed Oct 23, 2020
@Ethan-Arrowood
Copy link

@Ethan-Arrowood Ethan-Arrowood commented Jun 25, 2021

Hey folks I'm using standard to lint some Node 16 ESM files and I'm having an issue with TLA. Any solutions?

@nolanlawson
Copy link

@nolanlawson nolanlawson commented Jul 4, 2021

@Ethan-Arrowood I'm ignoring the files containing top-level await for now:

// package.json
"standard": {
  "ignore": [
    "**/the-filename.js"
  ],
}

@extremeheat
Copy link

@extremeheat extremeheat commented Jul 11, 2021

Top level await has been stage 4 for a while, is there any fix?

@NemoStein
Copy link

@NemoStein NemoStein commented Jul 13, 2021

@extremeheat You can track ESLint progress here: eslint/eslint#14632
It'll be released in ESLint 8.0.
Once ESLint is updated, Standard surely will follow shortly after.

@LinusU
Copy link
Member

@LinusU LinusU commented Aug 5, 2021

Nothing firm. We will likely release a beta within the next two weeks and the final version will depend on the feedback we get during the beta.

Hopefully this is happening soon! 🎉

@Divlo
Copy link
Member

@Divlo Divlo commented Aug 19, 2021

With the current code, we can't upgrade to eslint@8 when it will be released.
We're working on it: standard/standard-engine#234

I guess we could release standard@17 with eslint@8 to support all the new features, including Top-level await.
Also the first v8.0.0 beta prerelease has been published! (see: eslint/eslint#14872 and https://eslint.org/blog/2021/08/eslint-v8.0.0-beta.0-released).

@willfarrell
Copy link

@willfarrell willfarrell commented Jan 17, 2022

With AWS Lambda now supporting top level await (https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/) I imagine a lot more will run into this parsing error. It looks like eslint@8 (https://github.com/eslint/eslint/releases) has been released.

@NemoStein
Copy link

@NemoStein NemoStein commented Jan 17, 2022

@willfarrell you can follow Standard 17 (which uses ESLint 8) progress here: standard/eslint-config-standard#208

@voxpelli
Copy link
Member

@voxpelli voxpelli commented Jan 31, 2022

Prerelease 17.0.0-0 of standard is now released, containing ESLint 8 support, which should fix this issue. See: https://github.com/standard/standard/releases/tag/v17.0.0-0

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

@NemoStein
Copy link

@NemoStein NemoStein commented Jan 31, 2022

@voxpelli Something wrong is not right

// main.js
await new Promise(resolve => resolve())
$ standard --version 
17.0.0-0
$ eslint --version     
v8.8.0
$ standard .\main.js
standard: Use JavaScript Standard Style (https://standardjs.com)
  .\main.js:1:1: Parsing error: Cannot use keyword 'await' outside an async function (null)

Furthermore, VSCode (standard.vscode-standard v2.0.1) don't show Standard errors anymore.

@voxpelli
Copy link
Member

@voxpelli voxpelli commented Jan 31, 2022

@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 standard/eslint-config-standard#208 now 🙏

@voxpelli voxpelli reopened this Jan 31, 2022
@75lb
Copy link

@75lb 75lb commented Jan 31, 2022

just had a quick look and standard 17 doesn't support class instance fields, either (which eslint 8 does)

class Test {
  something = 1
}

export default Test
$ standard instance-field.js
standard: Use JavaScript Standard Style (https://standardjs.com)
  /Users/lloyd/Documents/instance-field:2:13: Parsing error: Unexpected token = (null)

@NemoStein
Copy link

@NemoStein NemoStein commented Jan 31, 2022

@voxpelli Ok, here goes my 2 cents.
Using only ESLint, if you don't set the parserOptions.ecmaVersion to "latest" it also triggers the error

// .eslintrc.js

module.exports = {
  env: {
    es2021: true,
  },
  parserOptions: {
    ecmaVersion: 'latest', // <--- This line!!!
    sourceType: 'module'
  }
}
// main.js

await new Promise(resolve => resolve())

With parserOptions.ecmaVersion: "latest"

$ eslint .\main.js
$

Without

$ eslint .\main.js

.\main.js
  1:1  error  Parsing error: Cannot use keyword 'await' outside an async function

✖ 1 problem (1 error, 0 warnings)

$

@voxpelli
Copy link
Member

@voxpelli voxpelli commented Jan 31, 2022

@NemoStein Yeah, I arrived at the same conclusion, will merge and release standard/eslint-config-standard#212 when another maintainer gives me a review 👍

@voxpelli
Copy link
Member

@voxpelli voxpelli commented Jan 31, 2022

@NemoStein @75lb We just released 17.0.0-1 with a fix for this.

Unless standard/eslint-config-standard-jsx#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-2 tomorrow.

Both me and @Divlo needs some sleep now 😅

@NemoStein
Copy link

@NemoStein NemoStein commented Feb 1, 2022

Unless standard/eslint-config-standard-jsx#43 overrides that fix

Sadly it does, but I can confirm that updating the eslint-config-standard-jsx/eslintrc.json to "ecmaVersion": 2022 works.

@voxpelli
Copy link
Member

@voxpelli voxpelli commented Feb 3, 2022

17.0.0-2 should finally be fixing this + we now have tests that ensures that ensures that's the case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment