JSHint and jQuery: '$' is not defined

JavascriptJqueryLintJshint

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

You can also add two lines to your .jshintrc

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

This tells jshint that there are two global variables.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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
  },
}

Solution 5 - Javascript

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.

Solution 6 - Javascript

Instead of recommending the usual "turn off the JSHint globals", I recommend using the http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript">module pattern to fix this problem. It keeps your code "contained" and gives a performance boost (based on Paul Irish's https://www.youtube.com/watch?v=i_qE1iAmjFg">"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 http://briancray.com/posts/javascript-module-pattern">here</a>;):

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

Solution 7 - Javascript

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.

Solution 8 - Javascript

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)

Solution 9 - Javascript

You probably want to do the following,

const $ = window.$

to avoid it throwing linting error.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAllyl IsocyanateView Question on Stackoverflow
Solution 1 - JavascriptStephen BooherView Answer on Stackoverflow
Solution 2 - JavascriptwmilView Answer on Stackoverflow
Solution 3 - JavascriptnirenView Answer on Stackoverflow
Solution 4 - JavascriptJames HarringtonView Answer on Stackoverflow
Solution 5 - JavascriptJoe GoltonView Answer on Stackoverflow
Solution 6 - JavascriptAlastair ParagasView Answer on Stackoverflow
Solution 7 - JavascriptknowncitizenView Answer on Stackoverflow
Solution 8 - JavascriptTony L.View Answer on Stackoverflow
Solution 9 - Javascriptnilakantha singh deoView Answer on Stackoverflow