48

I'm current developing an API on Node 12.14.1 and using Eslint to help me write the code. Unfortunately it does not allow me to set static class properties as shown below:

class AuthManager {
  static PROP = 'value'
}

The following error is given: Parsing error: Unexpected token =eslint

Static class properties are already supported on JS and on Node.
How can this rule be disable?

I also have the following .eslintrc.json file:

{
  "env": {
      "es6": true,
      "node": true
  },
  "extends": "eslint:recommended",
  "globals": {
      "Atomics": "readonly",
      "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
      "ecmaVersion": 2018,
      "sourceType": "module"
  }
}

4 Answers 4

68

ESLint v8 now supports static class properties natively: https://eslint.org/blog/2021/10/eslint-v8.0.0-released

parserOptions ecmaVersion should be set to 13, 2022, or "latest" to enable the support.

Add this to your .eslint.(cjs | json | js)

{
  parserOptions: {
    ecmaVersion: 2022,
  }
}
7
  • 3
    As of v8 being released, this is now the 'right' answer. Commented Oct 26, 2021 at 19:42
  • For some reason I'm still getting the same error on 8.6.0. Not sure if I'm the only one but if anyone reads this, know that you are not...
    – KyleFarris
    Commented Jan 10, 2022 at 19:08
  • FWIW, I ended up having to use @babel/eslint-parser as specified in other answers. Was trying to avoid using babel anywhere. But, at least it still works.
    – KyleFarris
    Commented Jan 10, 2022 at 19:32
  • 4
    Here is the configuration you need to add in your eslintrc config file (for a JSON file): { "parserOptions": { "ecmaVersion": "latest" } } Commented Jan 17, 2022 at 20:18
  • 1
    If you are targeting Node.js 14, do not set ecmaVersion to 2022. Only some of ES2022's features have been implemented in Node.js 14. Commented Aug 20, 2022 at 23:52
44

ESLint with its default parser does not support class fields syntax for now. You can solve the problem by changing the configured parser to babel-eslint.

npm install --save-dev babel-eslint
// eslintrc.json
{
  "parser": "babel-eslint",
  ...
}

Eslint's default parser, Espree, does not support class fields because that syntax is currently stage 3, and that it is decided that only stage 4 proposals are to be supported in Espree.

4
  • 1
    Thanks! After setting babel-eslint the problem was solved. Commented Mar 2, 2020 at 12:43
  • 19
    babel-eslint is now @babel/eslint-parser.
    – qntm
    Commented Oct 30, 2020 at 18:17
  • 1
    According to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… static is supported since node 6. I'm using node 14, the latest stable LTS version. It is not pretty clear for me what is the reason for ESLint to not support it yet.
    – Kostanos
    Commented Aug 26, 2021 at 12:30
  • @Kostanos according to TC39 Finished Proposals, Class Properties is a finished proposal and is included in ES2022. At the meantime, ESLint 7.x offically supports up to ES2020. This is why you have to use @babel/eslint-parser. Babel ESLint Parser covers all experimental syntax (lists here) that ESLint default parser doesn't support.
    – Wayne Mao
    Commented Sep 8, 2021 at 5:29
8

As of now, I had to use these configs

.eslintrc.js

module.exports = {
  env: {
    node: true,
    es6: true,
  },
  extends: [
    'airbnb-base',
  ],
  parser: '@babel/eslint-parser',
  parserOptions: {
    babelOptions: {
      configFile: './.babelrc',
    },
    ecmaVersion: 2018, // needed to support spread in objects
  },
  plugins: ['@babel'],
};

.babelrc

{
  "presets": ["@babel/env"],
  "plugins": [
    "@babel/plugin-syntax-class-properties"
  ]
}

For which I had to install:

npm i -D @babel/preset-env
npm i -D @babel/eslint-parser
npm i -D @babel/eslint-plugin
npm i -D @babel/plugin-syntax-class-properties

Notice that the @babel modules above are the only @babel modules in my package.json.

7

you need to install @babel/eslint-parser:

yarn add --dev @babel/eslint-parser

And have the parser in your .eslintrc.yml for instance:

parser: "@babel/eslint-parser"

Not the answer you're looking for? Browse other questions tagged or ask your own question.