Why does JSHint throw a warning if I am using const?

265,832

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.

  1. User Settings

  2. JSHint config

  3. "jshint.config": {} (Edit)

  4. 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

VSCode Settings

  • 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 */.

Solution

Share:
265,832
Andre Schlesinger
Author by

Andre Schlesinger

Updated on January 27, 2022

Comments

  • Andre Schlesinger
    Andre Schlesinger over 2 years

    This is the error I get when using const:

    <error line="2" column="1" severity="warning" message="&apos;const&apos; 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
    Scott Stensland over 8 years
    this works ... sure would rather it be some command line parm ! ... hint hint jshint folk
  • Olga
    Olga over 8 years
    The 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
    Josh Pittman over 8 years
    The 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
    cybersam about 8 years
    { "esnext": true } is now deprecated. You should now use { "esversion": 6 } instead.
  • Joncom
    Joncom over 7 years
    I 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
    Ethan Yang over 7 years
    Is there a configuration for using this? My jshint just ignores what is inside .jshintrc.
  • Zanon
    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
    JoeTidee about 7 years
    This works for those using a Grunt. Add this to gruntfile.js
  • Cyril Duchon-Doris
    Cyril Duchon-Doris about 7 years
    Then upgrade your JShint 😉
  • teknopaul
    teknopaul almost 7 years
    For me its .jshintrc not .jshint, not sure if there are differences in jshint versions
  • James Hibbard
    James Hibbard almost 7 years
    Oops. Right you are. Corrected.
  • Vitor Braga
    Vitor Braga over 6 years
    Then restart your Atom, if it is the case
  • CopyJosh
    CopyJosh over 6 years
    Thank you. Definitely the best answer here for those using visual studio code. For ref, User Settings via (Mac:) "Code" > Preferences > Settings.
  • sg28
    sg28 about 6 years
    Thank you ,worked like a charm for me .No need to ignore file(s) and individual implementation of esversioning in exclusive page(s)
  • Danielle
    Danielle over 4 years
    Thank you for this additional solution. it is very helpful.
  • Taylor D. Edmiston
    Taylor D. Edmiston over 4 years
    If 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
    ArifMustafa about 4 years
    Answer accepted in April, 2020 for Visual Studio Code
  • Bi Wu
    Bi Wu almost 4 years
    It 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
    John Zenith over 3 years
    Great, 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
    IgorAlves about 3 years
    that worked for me. this is great because we can use different versions across projects - if needed.
  • AlexLaforge
    AlexLaforge almost 2 years
    If 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 */.