teepusink teepusink - 2 years ago 120
Javascript Question

Grunt lint error with $

I'm trying to use lint with Grunt. I'm able to run Grunt from the command line but it is giving me a lot of errors. Mostly "'

' is not defined". Even
is throwing an error, "'alert' is not defined".

How can I get around those?

Answer Source

You need to tell JSHint (which is the linter that Grunt uses by default) about the global variables available to the files being linted. I'm assuming that you're including jQuery on your pages, hence the $ identifier (could be various other libraries of course).

You can either specify global variables in each file, or in the Grunt script. To specify them in a file, you can use a global directive. Place this at the top of the file, or at the top of the function in which you use the global:

/*global $:false */

Note that the false means you'll get errors if you override $. If you need the ability to do that, change it to true.

If you'd prefer to specify globals in the Grunt script, you can add a globals property to any of the tasks in your jshint section. For example:

    jshint: {
        someTask: {
            globals: {
                $: false

As for the alert message you're getting, you need to tell JSHint that you're allowing the use of development functions, such as alert and console.log. To do that, you can use a jshint directive in the files (just like the global directive):

/*jshint devel:true */

Or you can add an options property to the task in the Grunt script:

someTask: {
    globals: {
        $: false
    options: {
        devel: true

See the JSHint docs for all of the options available to you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download