Parsing error: Unexpected character '#' while declaring priavate member in JavaScript class

Viewed 1307

When using '#' to declare a private member in a javascript class, eslint throws an error Parsing error: Unexpected character '#'.

For eg.

class Test{
  #priavteMember; //Parsing error: Unexpected character '#'
}

eslint configuration: .eslintrc.json

{
    "env": {
        "browser": true,
        "es2021": true,
        "node": true
    },
    "extends": [
        "airbnb-base"
    ],
    "rules": {
        "no-use-before-define": "off",
        "no-param-reassign": "off",
        "no-plusplus": "off",
        "no-nested-ternary": "off",
        "lines-between-class-members": "off"
    },
    "globals": {
        "root": "readonly",
        "app": "readonly",
        "db": "readonly"
    }
}
2 Answers

Add this to .eslintrc.json

{
  ...
  "parserOptions": {
      "ecmaVersion": 13,
  },
  ...
}

Private member specifier was introduced in ECMA version 13.

Use the es2022 environment, which will automatically set the ecmaVersion parser option to 13, as indicated in the documentation:

    "env": {
        "browser": true,
        "es2022": true,
        "node": true
    },
Related