JSHint and jQuery: '$' is not defined

215

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?

This question is tagged with javascript jquery lint jshint

~ Asked on 2012-01-13 15:18:10

The Best Answer is


383

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

~ Answered on 2012-01-13 16:45:03


148

You can also add two lines to your .jshintrc

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

This tells jshint that there are two global variables.

~ Answered on 2013-07-19 15:40:13


Most Viewed Questions: