Reputation: 28882
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 alert
is throwing an error, "'alert' is not defined".
How can I get around those?
Upvotes: 2
Views: 2488
Reputation: 4198
globals
must be inside options
someTask: {
options: {
devel: true,
globals: {
$: false
}
}
}
Upvotes: 0
Reputation: 165941
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:
grunt.initConfig({
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.
Upvotes: 9