282

I am getting this error from ESLint:

error  Parsing error: The keyword 'const' is reserved

from this code:

const express = require('express');
const app = express();
const _ = require('underscore');

I've tried removing node_modules and reinstalling all npm packages (as suggested here), but to no avail.

MultiplyByZer0
  • 5,209
  • 3
  • 29
  • 46
opike
  • 6,347
  • 14
  • 62
  • 92

8 Answers8

429

ESLint defaults to ES5 syntax-checking.
You'll want to override to the latest well-supported version of JavaScript.

Try adding a .eslintrc.json file to your project. Inside it:

{
    "parserOptions": {
        "ecmaVersion": 2017
    },

    "env": {
        "es6": true
    }
}

Hopefully this helps.

EDIT: I also found this example .eslintrc.json which might help.

Henke
  • 2,831
  • 2
  • 16
  • 26
iamjpg
  • 5,962
  • 1
  • 15
  • 17
  • 1
    These options are explained in this official documentation: Language Options - ESLint - https://eslint.org/docs/user-guide/configuring/language-options – shuuji3 Sep 04 '21 at 16:41
22

you also can add this inline instead of config, just add it to the same file before you add your own disable stuff

/* eslint-env es6 */
/* eslint-disable no-console */

my case was disable a file and eslint-disable were not working for me alone

/* eslint-env es6 */
/* eslint-disable */
yousef
  • 1,044
  • 10
  • 13
  • This should be the accepted answer imo, much easier to implement since you might not be able to easily edit your eslint config, like for scaffolded frameworks, group projects and such – Dudeonyx Aug 14 '21 at 11:12
  • /* eslint-env es2020 */ is usable too. https://eslint.org/docs/user-guide/configuring/language-options list them all. Good answer, as I need it only on JS config files, the other files are TypeScript with dedicated parser. – PhiLho Jan 07 '22 at 07:43
  • I implemented the accepted answer, however it did not solve my problem in my karma.config.ci.js file. Only after writing ```/* eslint-env es6 */``` in the first line of code, the problem went away. Thanks! – MikhailRatner Mar 21 '22 at 11:23
15

I used .eslintrc.js and I have added following code.

module.exports = {
    "parserOptions": {
        "ecmaVersion": 6
    }
};
Khachornchit Songsaen
  • 2,038
  • 22
  • 18
9

If using Visual Code one option is to add this to the settings.json file:

"eslint.options": {
    "useEslintrc": false,
    "parserOptions": {
        "ecmaVersion": 2017
    },
    "env": {
        "es6": true
    }
}
9

Update - ESLint v7.30.0

With ESLint v7.30.0, you can use latest instead of 2017, and it will enable the latest supported ECMAScript version.

"ecmaVersion": "latest" always enables the latest supported ECMAScript version in ESLint's default parser.

.eslintrc.json

"parserOptions": {
  "ecmaVersion": "latest"
}
NeNaD
  • 10,308
  • 5
  • 21
  • 59
1

I had this same problem with this part of my code:

const newComment = {
    dishId: dishId,
    rating: rating,
    author: author,
    comment: comment
};
newComment.date = new Date().toISOString();

Same error, const is a reserved word.

The thing is, I made the .eslintrc.js from the link you gave in the update and still got the same error. Also, I get an parsing error in the .eslintrc.js: Unexpected token ':'.

Right in this part:

"env": {
"browser": true,
"node": true,
"es6": true
},

...
DarkSuniuM
  • 2,373
  • 1
  • 24
  • 41
-1

I had this issue when updating. I had an eslintrc.json in the project already as well. I just closed my project in Visual Studio Code and reopened it and the error went away. It seems VS Code caches.

Curtis M
  • 587
  • 1
  • 5
  • 14
-2

In my case, it was unable to find the .eslintrc file so I copied from node_modules/.bin to root.