josemigallas josemigallas - 10 months ago 138
Node.js Question

jshint error when using template strings in VSCode

I'm using Visual Studio Code (1.6.1) with its jshint extension (0.10.15).

When I type some template string, such as:

console.log(`My name is: ${name}`);

VSCode highlights it in red and says:

[jshint] Unexpected '`'. (E024)

And the rest of the code starts showing dozens of false errors like:

[jshint] Expected an identifier and instead saw 'if'. (E030)
[jshint] Expected an operator and instead saw '('. (E030)
[jshint] Expected an assignment or function call and instead saw an expression. (W030)

I've looked in the docs, but nothing references this issue.

Anyone come up with a workaround for this?

Answer Source

It seems that your JSHint is not recognicing ES6.

Can you try to set esversion to 6 in JSHint options?

I am using Visual Studio Code 1.7.1, with jshint extension 0.10.15. Without .jshintrc I got this message:

[jshint] 'template literal syntax' is only available in ES6 (use 'esversion: 6'). (W119)

I created a .jshintrc file with content:

    "esversion": 6

And it worked.

Note that jshint module might be required (globally or locally), it can be automatically added to your package.json by doing:

npm install --save-dev jshint

This installs jshint locally (in node_modules folder) and adds a dev dependency entry:

  "devDependencies": {
    "jshint": "^2.9.4"

Note also that a restart of VS Code may be required, after installing the module or the extension.