Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eslint: error Parsing error: The keyword 'const' is reserved

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.

like image 771
opike Avatar asked Oct 06 '22 23:10

opike


5 Answers

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.

like image 462
iamjpg Avatar answered Oct 23 '22 22:10

iamjpg


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 */
like image 25
yousef Avatar answered Oct 23 '22 20:10

yousef


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

module.exports = {
    "parserOptions": {
        "ecmaVersion": 6
    }
};
like image 15
Khachornchit Songsaen Avatar answered Oct 23 '22 20:10

Khachornchit Songsaen


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"
}
like image 12
NeNaD Avatar answered Oct 23 '22 22:10

NeNaD


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
    }
}
like image 10
Bjørnar Hvidsten Avatar answered Oct 23 '22 20:10

Bjørnar Hvidsten