Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint: First instance of let results in "unexpected token" error [duplicate]

My javascript:

let foo = 'bar'

Why does ESLint respond with the following?

~/index.js
  1:5  error  Parsing error: Unexpected token foo

✖ 1 problem (1 error, 0 warnings)

It seems like no matter where in the script, the first instance of using let to set a variable gets this error. Why??

My .eslintrc file:

module.exports = {
    "env": {
        "node": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "never"
        ]
    }
};
like image 872
wise_gremlin Avatar asked Nov 29 '16 20:11

wise_gremlin


1 Answers

The answers about let being forbidden in the global scope are wrong. That's fine.

The problem is that you need to let eslint know you're using es6 features.

Do that by adding an "es6": true line to the env block in your config:

.eslintrc.js

module.exports = {
    "env": {
        "node": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "never"
        ]
    }
};

That fixes your particular error, but you'll soon see errors about unused names. Below is the minimum source code I could find that will pass your eslint settings:

let.js

let foo = 'bar'

function main() {
  foo
}

main()
like image 187
Kenan Banks Avatar answered Nov 08 '22 18:11

Kenan Banks