admin管理员组

文章数量:1205167

I have a Node.js web application where I use Backbone.js and jQuery. My main js file is app.js where I include all the necessary scripts, including jQuery. So, this is the beginning of my app.js code:

'use strict';

var _ = require('lodash');
var $ = require('jquery');
var B = require('backbone');
B.$ = $;

Now if I run grunt on my project, it raises errors at the line where jQuery is loaded to $. It shows me this:

Linting public/js/app.js ...ERROR
[L4:C5] W079: Redefinition of '$'.
var $ = require('jquery');

I can still get everything running with grunt --force, but I would like to eliminate this error anyway. Can somebody explain why it raises the error and how to fix it?


My .jshintrc file:

{
    "laxcomma": true
    ,"laxbreak": true
    ,"curly": true
    ,"eqeqeq": true
    ,"immed": true
    ,"latedef": true
    ,"newcap": true
    ,"noarg": true
    ,"sub": true
    ,"undef": true
    ,"unused": false
    ,"asi": true
    ,"boss": true
    ,"eqnull": true
    ,"node": true
    ,"browser": true
    ,"jquery": true
    ,"predef":
    [
        "suite"
        ,"test"
        ,"setup"
        ,"teardown"
        ,"suiteSetup"
        ,"suiteTeardown"
        ,"requestAnimationFrame"
    ]
}

I have a Node.js web application where I use Backbone.js and jQuery. My main js file is app.js where I include all the necessary scripts, including jQuery. So, this is the beginning of my app.js code:

'use strict';

var _ = require('lodash');
var $ = require('jquery');
var B = require('backbone');
B.$ = $;

Now if I run grunt on my project, it raises errors at the line where jQuery is loaded to $. It shows me this:

Linting public/js/app.js ...ERROR
[L4:C5] W079: Redefinition of '$'.
var $ = require('jquery');

I can still get everything running with grunt --force, but I would like to eliminate this error anyway. Can somebody explain why it raises the error and how to fix it?


My .jshintrc file:

{
    "laxcomma": true
    ,"laxbreak": true
    ,"curly": true
    ,"eqeqeq": true
    ,"immed": true
    ,"latedef": true
    ,"newcap": true
    ,"noarg": true
    ,"sub": true
    ,"undef": true
    ,"unused": false
    ,"asi": true
    ,"boss": true
    ,"eqnull": true
    ,"node": true
    ,"browser": true
    ,"jquery": true
    ,"predef":
    [
        "suite"
        ,"test"
        ,"setup"
        ,"teardown"
        ,"suiteSetup"
        ,"suiteTeardown"
        ,"requestAnimationFrame"
    ]
}
Share Improve this question edited Jul 2, 2014 at 10:44 Terry asked Jul 1, 2014 at 12:51 TerryTerry 14.9k15 gold badges64 silver badges90 bronze badges 4
  • I'm going to guess you use grunt-contrib-jshint (Grunt by itself doesn't do code validation) -- edit if I'm wrong. – JJJ Commented Jul 1, 2014 at 13:49
  • 2 @Terry - can you post your JSHint config? You shouldn't receive this warning by default. – James Allardice Commented Jul 1, 2014 at 14:13
  • Yes, this is a definitely a jshint configuration issue. – Benjamin Gruenbaum Commented Jul 1, 2014 at 17:58
  • I updated the question with my .jshintrc file content. Anything unusual there? – Terry Commented Jul 2, 2014 at 10:44
Add a comment  | 

4 Answers 4

Reset to default 17 +50

In jshint docs: http://www.jshint.com/docs/options/

jquery = true 
This option defines globals exposed by the jQuery JavaScript library.

You are telling jshint that jquery exists, thus he assumes that $ is defined. Remove the "jquery" : true" and your issue should go away.

Add this at the top of the file to make that error go away:

/* jshint -W079 */

What's happening here is that JQuery defines the $ variable, so JSHint views this as a piece of "potentially dangerous code"

A better solution would be to require jquery directly which should give you access to the $ variable globally.

Random guess: do you include jquery somewhere else? For example in some html file as a script. Or some other script might be defining global jquery variable.

Make sure that in your .jshintrc you don't have '$' set as predefined.

// Predefined Globals
"predef" : ["$"]   

If it is, remove.

本文标签: javascriptjshint error Redefinition of 3939Stack Overflow