391

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.

Zanon
  • 29,231
  • 20
  • 113
  • 126
Andre Schlesinger
  • 3,925
  • 2
  • 12
  • 6
  • I don't see any question in the text, but probably you've already answered it: `"use esnext option"`. And why? Isn't that error message pretty clear? `const` is not standard JS (yet). – Teemu Dec 12 '14 at 10:45
  • 7
    or use eslint instead of jshint – Rakka Rage Sep 14 '17 at 15:09
  • Not sure if this is new, but the easier solution is to set the ECMAScript level to 6 in the settings. See my full answer below. – xtempore Feb 25 '21 at 22:51

19 Answers19

667

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
}
James Hibbard
  • 16,490
  • 14
  • 62
  • 74
  • 1
    this works ... sure would rather it be some command line parm ! ... hint hint jshint folk – Scott Stensland Sep 28 '15 at 00:16
  • 9
    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. – Olga Dec 29 '15 at 13:30
  • 6
    For me its .jshintrc not .jshint, not sure if there are differences in jshint versions – teknopaul Jun 15 '17 at 13:31
  • 1
    Oops. Right you are. Corrected. – James Hibbard Jun 15 '17 at 17:54
  • 1
    Then restart your Atom, if it is the case – Vitor Braga Oct 05 '17 at 00:27
  • 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? – Taylor D. Edmiston Oct 09 '19 at 01:11
  • 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. https://ibb.co/cDRc154 I hope it helps you too! – iconique May 30 '20 at 02:08
314

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.

James Hibbard
  • 16,490
  • 14
  • 62
  • 74
Zanon
  • 29,231
  • 20
  • 113
  • 126
  • 1
    Is there a configuration for using this? My jshint just ignores what is inside `.jshintrc`. – Ethan Yang Dec 08 '16 at 01:42
  • 2
    @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)` – Zanon Dec 08 '16 at 10:46
  • 1
    that worked for me. this is great because we can use different versions across projects - if needed. – IgorAlves Apr 13 '21 at 15:06
101

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

Nicholas Gentile
  • 1,512
  • 1
  • 9
  • 16
  • 10
    Thank you. Definitely the best answer here for those using visual studio code. For ref, User Settings via (Mac:) "Code" > Preferences > Settings. – CopyJosh Nov 14 '17 at 21:01
  • 1
    Thank you for this additional solution. it is very helpful. – Dani Aug 20 '19 at 02:33
  • 1
    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. – John Zenith Oct 03 '20 at 12:26
57

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.
Philip
  • 3,486
  • 1
  • 24
  • 37
38

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

Zanon
  • 29,231
  • 20
  • 113
  • 126
Josh Pittman
  • 7,024
  • 7
  • 38
  • 66
  • 24
    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 }``` – Josh Pittman Feb 16 '16 at 11:39
  • 18
    `{ "esnext": true }` is now [deprecated](http://jshint.com/docs/options/#esnext). You should now use `{ "esversion": 6 }` instead. – cybersam Mar 30 '16 at 23:48
  • 3
    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. – Joncom Sep 15 '16 at 10:54
  • 4
    Then upgrade your JShint – Cyril Duchon-Doris Apr 13 '17 at 15:12
22

You can specify esversion:6 inside jshint options object. Please see the image. I am using grunt-contrib-jshint plugin.

enter image description here

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
Wenlong Jiang
  • 722
  • 6
  • 5
  • 2
    This works for those using a Grunt. Add this to gruntfile.js – JoeTidee Feb 19 '17 at 01:18
  • 1
    Thank you ,worked like a charm for me .No need to ignore file(s) and individual implementation of esversioning in exclusive page(s) – sg28 Feb 28 '18 at 20:33
12

Create .jshintrc file in the root dir and add there the latest js version: "esversion": 9 and asi version: "asi": true (it will help you to avoid using semicolons)

{
    "esversion": 9,
    "asi": true
}
JohnPix
  • 1,595
  • 21
  • 44
7

When you start using ECMAScript 6 this error thrown by your IDE.

There are two options available:

if you have only one file and want to use the es6 then simply add below line at the top of the file.

/*jshint esversion: 6 */

Or if you have number of js file or you are using any framework(like nodejs express)you can create a new file named .jshintrc in your root directory and add code below in the file:

{
    "esversion": 6
}

If you want to use the es6 version onward for each project you can configure your IDE.

Prashant Barve
  • 4,105
  • 2
  • 33
  • 42
  • 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 */`. – AlexLaforge Jun 06 '22 at 18:06
7

In your package.json you can tell Jshint to use es6 like this

"jshintConfig":{
    "esversion": 6 
}
Singhak
  • 8,508
  • 2
  • 31
  • 34
5

Creating a .jshintrc file is not necessary.

If you are using ECMAScript 6 then all you need to do is tell JSHint that:

  1. Go to File > Settings
  2. Navigate to Languages & Frameworks > JavaScript > Code Quality Tools > JSHint.
  3. Scroll down to find Warn about incompatibilities with the specified ECMAScript version.
  4. Click on Set.
  5. Enter 6 and then press [Set].
  6. Click [OK]
xtempore
  • 5,260
  • 4
  • 36
  • 43
  • 1
    I think you should say this is for JetBrains users. I imagine WebStorm or PHPStorm. It worked for me using this last one. – Enrique Nov 04 '22 at 13:02
3

For SublimeText 3 on Mac:

  1. Create a .jshintrc file in your root directory (or wherever you prefer) and specify the esversion:
    # .jshintrc
    {
      "esversion": 6
    }
  1. Reference the pwd of the file you just created in SublimeLinter user settings (Sublime Text > Preference > Package Settings > SublimeLinter > Settings)
    // SublimeLinter Settings - User
    {
      "linters": {
        "jshint": {
          "args": ["--config", "/Users/[your_username]/.jshintrc"]
        }
      }
    }
  1. Quit and relaunch SublimeText
QuickSilver
  • 3,915
  • 2
  • 13
  • 29
laptite
  • 31
  • 2
2

If you are using Webstorm and if you don't have your own config file, then just enable EcmaScript.next in Relaxing options in in

Settings | Languages & Frameworks | JavaScript | Code Quality Tools | JSHint

See this question How-do-I-resolve-these-JSHint-ES6-errors

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94
2

If you are using Grunt configuration, You need to do the following steps

Warning message in Jshint:

enter image description here

Solution:

  1. Set the jshint options and map the .jshintrc.js file

enter image description here

  1. Create the .jshintrc.js file in that file add the following code
{  
  "esversion": 6  
} 

After configured this, Run again It will skip the warning,

enter image description here

Sridhar
  • 401
  • 7
  • 10
0

Create a file called, say jshint_opts with this content: { "esversion": 6 }

Then invoke jshint with something like this command line:

jshint --config jshint_opts lib/*.js

Alan Wendt
  • 17
  • 3
0

May 2020 Here's a simple solution i found and it will resolve for all of my projects ,on windows if your project is somewhere inside c: directory , create new file .jshintrc and save it in C directory open this .jshintrc file and write { "esversion": 6} and that's it. the warnings should go away , same will work in d directory

enter image description here

enter image description here yes you can also enable this setting for the specific project only by same creating a .jshintrc file in your project's root and adding { "esversion": 6}

Kunal Rajput
  • 664
  • 1
  • 7
  • 21
0

To fix this in Dreamweaver CC 2018, I went to preferences, edit rule set - select JS, edit/apply changes, find "esnext" and changed the false setting to true. It worked for me after hours of research. Hope it helps others.

0

I had the same issue, and I found that by adding:

/* jshint esversion: 8 */

(or whatever jshint esversion you need, like 6)

To the top of my .js file satisfies the cause for the warnings.

krazykaigh
  • 13
  • 3
-1

If using Sublime Text 3:

  • Go to Preferences -> Settings
  • Under Preferences.sublime-settings—User add "esversion": 6
London804
  • 1,072
  • 1
  • 22
  • 47
-2

In a new version of Dreamweaver to solve this error

  • Go to Edit->Preference->Linting
  • And the go-to js Edit rule set and past

    "jshintConfig":{ "esversion": 6 }