wise_gremlin wise_gremlin - 11 days ago 6
Javascript Question

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

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"
]
}
};

Answer

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 fine that will pass your eslint settings:

let.js

let foo = 'bar'

function main() {
  foo
}

main()