336

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.

Henke
  • 4,445
  • 3
  • 31
  • 44
opike
  • 7,053
  • 14
  • 68
  • 95

10 Answers10

528

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": "latest"
    },

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

Hopefully this helps.

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

iamjpg
  • 7,078
  • 1
  • 16
  • 18
  • 2
    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
  • @shuuji3 when it comes to find a solution for an error message, that documentation is useless....Even if you go through all the options, it won't tell you what triggered the error. – vanowm Aug 22 '22 at 00:28
  • 1
    Sorry, I think that my intention was just to provide the official documentation URL for reference to help to understand the answer. – shuuji3 Aug 24 '22 at 06:52
  • I had to rename the file where the const export is happening (source library) to be `.cjs` instead of `.js`. – Setup Jul 11 '23 at 13:13
31

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,240
  • 12
  • 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
  • /* eslint-env es6 */ is working fine to resolved const error but /* eslint-disable no-console */ is disabled all logs – Syed Tabish Ali Jun 28 '22 at 11:29
  • if you need something like spread operators (which weren't usable aside from in array literals and function calls until ECMAScript 2018 (ES9)), you can add newer versions of eslint to the definition like this: `/* eslint-env es6, es2017, es2018 */` – doteth May 16 '23 at 20:56
17

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
  • 18,172
  • 8
  • 47
  • 89
14

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

module.exports = {
    "parserOptions": {
        "ecmaVersion": 6
    }
};
KHACHORNCHIT
  • 2,222
  • 23
  • 19
11

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
    }
}
Bjørnar Hvidsten
  • 859
  • 12
  • 15
2

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
},

...
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
1

I used the config in .eslintrc.json as mentioned in the accepted answer:

{
  parserOptions: {
    "ecmaVersion": "latest"
  },
  env: {
    "es6": true
  }
}

It was also necessary to add to settings.json:

"eslint.options": { "configFile": "eslintrc.json" }
Mike
  • 63
  • 5
1

In case none of the answers here helped, I faced a similar issue, and what I was missing from the .eslintrc.json file was the following:

{
 "overrides": [
    {
      // rest of file...

      "files": [
        "*.ts",
        "*.js" // <- this part
      ],

       // rest of file...
 }]
}

Hope this helps!

Ron Strauss
  • 60
  • 1
  • 7
0

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
  • 905
  • 1
  • 8
  • 14
-5

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