220

The following JS:

(function() {
  "use strict";

  $("#target").click(function(){
    console.log("clicked");
  });

}());

Yields:

test.js: line 5, col 3, '$' is not defined.

When linted using JSHint 0.5.5. Any ideas?

Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130

9 Answers9

391

If you are using a relatively recent version of JSHint, the generally preferred approach is to create a .jshintrc file in the root of your project, and put this config in it:

{
    "globals": {
        "$": false
    }
}

This declares to JSHint that $ is a global variable, and the false indicates that it should not be overridden.

The .jshintrc file was not supported in really old versions of JSHint (such as v0.5.5 like the original question in 2012). If you cannot or do not want to use the .jshintrc file, you can add this at the top of the script file:

/*globals $:false */

There is also a shorthand "jquery" jshint option as seen on the JSHint options page..

Étienne
  • 4,773
  • 2
  • 33
  • 58
Stephen Booher
  • 6,522
  • 4
  • 34
  • 50
  • 47
    This option, jquery, simply defines two read-only global variables: $ and jQuery. It is a shorter version of `/*global $:false, jQuery:false */`. – Anton Kovalyov Jan 13 '12 at 19:24
  • 1
    With my JSHint for Visual Studio, the option is called "assume jQuery" – Jowen May 07 '14 at 08:09
  • 2
    The option flag is the way to go.For first timers: add the line 'jquery: true' in your .jshintrc file. – genkilabs Aug 21 '14 at 15:38
  • 3
    What is the reason for ":false"?, I found /*global $ */ works just fine. (In my case in was /*global jQuery */ though) – Michiel Oct 21 '14 at 08:42
  • is there a way to set this generically for all functions within a script, rather than type one by one in `/* global ... */`? I typically import a script with all my functions into my `main` so it is cumbersome to define one by one... – tsando Sep 25 '17 at 09:33
154

You can also add two lines to your .jshintrc

  "globals": {
    "$": false,
    "jQuery": false
  }

This tells jshint that there are two global variables.

James McMahon
  • 48,506
  • 64
  • 207
  • 283
wmil
  • 3,179
  • 2
  • 21
  • 24
  • 3
    this should be the accepted answer. polluting EVERY SINGLE javascript FILE in your project with various lines of code to make the a syntax checker happy is not an option. Thanks wmil – Cosmin Feb 24 '15 at 21:32
54

All you need to do is set "jquery": true in your .jshintrc.

Per the JSHint options reference:

jquery

This option defines globals exposed by the jQuery JavaScript library.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
niren
  • 2,693
  • 8
  • 34
  • 58
  • 6
    This should probably be the top answer... shame it's buried down here – Zach Lysobey Feb 26 '15 at 03:28
  • Where to find .jshintrc file in Sublime text 3 ? I tried to put this option in SublimeLinter.sublime-settings file (Preferences->Package Settings->SublimeLinter->Settings-Default) and also tried to make new .jshintrc file (my.jshintrc) in my project root directory, according to http://jshint.com/docs/. Non of this helped me. – Milos Dec 17 '15 at 00:22
  • @Milos Install JSHint Gutter in sublimetext 3. Go to Tools > JSHint > Set Linting Preferences, It'll open .jshintrc. – niren Dec 18 '15 at 09:23
  • 1
    Nothing again, it still cannot recognizes "$" sign. When I open .jshintrc it is on path: \AppData\Roaming\Sublime Text 3\Packages\JSHint Gutter, I put "jquery": true and nothing happens. – Milos Dec 22 '15 at 08:01
  • Much better answer than adding variables to the globals section! Solve it this way! – Matthias Kleine Aug 30 '16 at 10:30
17

Here is a happy little list to put in your .jshintrc
I will add to this list at time passes.

{
  // other settings...
  // ENVIRONMENTS
  // "browser": true, // Is in most configs by default
  "node": true,
  // others (e.g. yui, mootools, rhino, worker, etc.)
  "globals": {
    "$":false,
    "jquery":false,
    "angular":false
    // other explicit global names to exclude
  },
}
James Harrington
  • 3,138
  • 30
  • 32
  • 1
    If you set the "browser" and "node" options to "true", the only remaining items in this list that are necessary are $, jquery, and angular. For more info, see the Environment section of the docs: http://www.jshint.com/docs/options/#environments – user456584 May 27 '14 at 23:49
  • I've edited the answer to include your update thank you @user456584 – James Harrington May 29 '14 at 13:32
9

If you're using an IntelliJ editor such as WebStorm, PyCharm, RubyMine, or IntelliJ IDEA:

In the Environments section of File/Settings/JavaScript/Code Quality Tools/JSHint, click on the jQuery checkbox.

Joe Golton
  • 612
  • 6
  • 12
  • 1
    Pay attention as well that you can override all inner configurations by checking "use config file". This will make WS see the config file modifications. Without it he just ignores it. – neoswf May 12 '15 at 13:44
3

Instead of recommending the usual "turn off the JSHint globals", I recommend using the module pattern to fix this problem. It keeps your code "contained" and gives a performance boost (based on Paul Irish's "10 things I learned about Jquery").

I tend to write my module patterns like this:

(function (window) {
    // Handle dependencies
    var angular = window.angular,
        $ = window.$,
        document = window.document;

    // Your application's code
}(window))

You can get these other performance benefits (explained more here):

  • When minifying code, the passed in window object declaration gets minified as well. e.g. window.alert() become m.alert().
  • Code inside the self-executing anonymous function only uses 1 instance of the window object.
  • You cut to the chase when calling in a window property or method, preventing expensive traversal of the scope chain e.g. window.alert() (faster) versus alert() (slower) performance.
  • Local scope of functions through "namespacing" and containment (globals are evil). If you need to break up this code into separate scripts, you can make a submodule for each of those scripts, and have them imported into one main module.
  • 2
    This is a pattern I use as well, but it does not solve the original problem for me. JSHint still complains that a variable is not defined on the last line (`"}(window))`" in your example -- I had issues also with jQuery and Highcharts). I haven't found a way to turn it off other than defining a global for the file, which then defeats the purpose. – myrosia Aug 27 '14 at 11:04
  • @myrosia `browser: true` tells JSHint that you expect a browser environment (where `window` is already defined). – sam Jun 04 '16 at 19:56
  • I like this approach! – jethroo Sep 15 '16 at 09:16
2

If you're using an IntelliJ editor, under

  • Preferences/Settings
    • Javascript
      • Code Quality Tools
        • JSHint
          • Predefined (at bottom), click Set

You can type in anything, for instance console:false, and it will add that to the list (.jshintrc) as well - as a global.

knowncitizen
  • 1,523
  • 2
  • 17
  • 21
2

To fix this error when using the online JSHint implementation:

  • Click "CONFIGURE" (top of the middle column on the page)
  • Enable "jQuery" (under the "ASSUME" section at the bottom)
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Tony L.
  • 17,638
  • 8
  • 69
  • 66
0

You probably want to do the following,

const $ = window.$

to avoid it throwing linting error.