JSHint and jQuery: '$' is not defined

Allyl Isocyanate picture Allyl Isocyanate · Jan 13, 2012 · Viewed 110k times · Source

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?

Answer

Stephen Booher picture Stephen Booher · Jan 13, 2012

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