JSHint and jQuery: '$' is not defined

Solution 1:

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

Solution 2:

You can also add two lines to your .jshintrc

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

This tells jshint that there are two global variables.