Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jshint error: Redefinition of '$'

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"
    ]
}
like image 835
Terry Avatar asked Jul 01 '14 12:07

Terry


2 Answers

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.

like image 95
fmsf Avatar answered Oct 19 '22 03:10

fmsf


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.

like image 37
ctlacko Avatar answered Oct 19 '22 03:10

ctlacko