Why does JSHint throw a warning if I am using const?
Solution 1
When relying upon ECMAScript 6 features such as const
, you should set this option so JSHint doesn't raise unnecessary warnings.
/*jshint esnext: true */ (Edit 2015.12.29: updated syntax to reflect @Olga's comments)
/*jshint esversion: 6 */
const Suites = {
Spade: 1,
Heart: 2,
Diamond: 3,
Club: 4
};
This option, as the name suggests, tells JSHint that your code uses ECMAScript 6 specific syntax. http://jshint.com/docs/options/#esversion
Edit 2017.06.11: added another option based on this answer.
While inline configuration works well for an individual file, you can also enable this setting for the entire project by creating a .jshintrc
file in your project's root and adding it there.
{
"esversion": 6
}
Solution 2
You can add a file named .jshintrc in your app's root with the following content to apply this setting for the whole solution:
{
"esversion": 6
}
James' answer suggests that you can add a comment /*jshint esversion: 6 */
for each file, but it is more work than necessary if you need to control many files.
Solution 3
I got this same warning when using an export statement. I'm using VS Code and used a similar approach to Wenlong Jiang's solution.
-
User Settings
-
JSHint config
-
"jshint.config": {}
(Edit) -
Use double quotes when specifying
"esversion"
Or copy this snippet into User Settings:
"jshint.options": { "esversion": 6, }
Creating a .jshintrc
file isn't necessary if you want to configure the global jshint settings for your editor
Solution 4
If you're using VSCode:
1.
- Go to preferences -> settings (
cmd + ,
) - Type
jshint.options
into the search bar - Hover over it and click on the pencil icon
- Its now appended on the right side.
- Add
"esversion": 6
to the options object.
2.
Or simply add this to your user settings:
"jshint.options": {
"esversion": 6
}
[UPDATE] new vscode settings
- Go to preferences -> settings (
cmd + ,
) - type
jshint
into search
- continue with step
2.
Solution 5
I spent ages trying to fix this. Every solution talks about 'setting options'. I don't know what that means. Finally, I figured it out. You can just include a commented out line at the top of the file /*jshint esversion: 6 */
.
Andre Schlesinger
Updated on January 27, 2022Comments
-
Andre Schlesinger over 2 years
This is the error I get when using const:
<error line="2" column="1" severity="warning" message="'const' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)." source="jshint.W104" />
My code looks like this:
const Suites = { Spade: 1, Heart: 2, Diamond: 3, Club: 4 };
The code works fine only JSHint is warning me every time.
-
Scott Stensland over 8 yearsthis works ... sure would rather it be some command line parm ! ... hint hint jshint folk
-
Olga over 8 yearsThe documentation now says
Warning This option has been deprecated and will be removed in the next major release of JSHint. Use esversion: 6 instead.
Although my Webstorm built-in plugin does not recognize new option. I ended up specifying both. -
Josh Pittman over 8 yearsThe only problem with doing it this way , as I have found out, is that you have to add the comment to the top of every file you use. A better solution is to create a new file in the root folder of you app named
.jshintrc
and then add the following code to the file{ "esnext": true }
-
cybersam about 8 years
-
Joncom over 7 yearsI must be using an out-dated version of JSHint, because only by using
"esnext": true
was I able to solve this."esversion": 6
had no effect. -
Ethan Yang over 7 yearsIs there a configuration for using this? My jshint just ignores what is inside
.jshintrc
. -
Zanon over 7 years@EthanYang, No. There is no extra configuration. Just make sure that the file can be found by JSHint. From docs:
JSHint will start looking for this file in the same directory as the file that's being linted. If not found, it will move one level up the directory tree all the way up to the filesystem root. (Note that if the input comes from stdin, JSHint doesn't attempt to find a configuration file)
-
JoeTidee about 7 yearsThis works for those using a Grunt. Add this to gruntfile.js
-
Cyril Duchon-Doris about 7 yearsThen upgrade your JShint 😉
-
teknopaul almost 7 yearsFor me its .jshintrc not .jshint, not sure if there are differences in jshint versions
-
James Hibbard almost 7 yearsOops. Right you are. Corrected.
-
Vitor Braga over 6 yearsThen restart your Atom, if it is the case
-
CopyJosh over 6 yearsThank you. Definitely the best answer here for those using visual studio code. For ref, User Settings via (Mac:) "Code" > Preferences > Settings.
-
sg28 about 6 yearsThank you ,worked like a charm for me .No need to ignore file(s) and individual implementation of esversioning in exclusive page(s)
-
Danielle over 4 yearsThank you for this additional solution. it is very helpful.
-
Taylor D. Edmiston over 4 yearsIf anyone else lands here after seeing the error on JSBin, the answer above does not work (It gives the warning:
Bad option: 'esversion'.
) The old answer does still work there though:/*jshint esnext: true*/
. Maybe they are using an old version of something? -
ArifMustafa about 4 yearsAnswer accepted in April, 2020 for Visual Studio Code
-
Bi Wu almost 4 yearsIt works! Thanks, but make sure you should check the location setting for .jshintrc file. I use PhpStorm, which has an option to customize the location of that file. ibb.co/cDRc154 I hope it helps you too!
-
John Zenith over 3 yearsGreat, many thanks. If you use VS Code, this works so well. Also, note that in user settings, this is now
jshint.config
, just in case. -
IgorAlves about 3 yearsthat worked for me. this is great because we can use different versions across projects - if needed.
-
AlexLaforge almost 2 yearsIf this
/*jshint esversion: 6 */
leads to a warning (for example in Sublime Text older versions, like Sublime Text v2), you can then use/*jshint esnext: true */
.