jshint错误:重新定义'$'

我有一个Node.js Web应用程序,我使用Backbone.js和jQuery。 我的主要js文件是app.js ,其中包括所有必需的脚本,包括jQuery。 所以,这是我的app.js代码的开始:

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

现在,如果我在我的项目上运行grunt ,它会在jQuery加载到$的行上引发错误。 它显示了我:

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

我仍然可以使用grunt --force运行所有的东西,但是我想要消除这个错误。 有人可以解释为什么会引发错误,以及如何解决?


我的.jshintrc文件:

 { "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" ] } 

在jshint文档中: http : //www.jshint.com/docs/options/

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

你告诉jshint jQuery存在,因此他假设$是定义的。 删除"jquery" : true" ,你的问题应该消失。

在文件的顶部添加它以使错误消失:

/* jshint -W079 */

这里发生的事情是JQuery定义了$variables,所以JSHint将其视为一段“潜在危险的代码”

一个更好的解决scheme将是要求直接jQuery的应该让你访问全局$variables。

随机猜测:你在其他地方包含jquery吗? 例如在一些html文件中作为脚本。 或者其他一些脚本可能会定义全局的jQueryvariables。

确保你的.jshintrc没有按照预定义设置'$'。

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

如果是,请删除。