javascript - jshint 错误 : Redefinition of '$'

标签 javascript jquery node.js backbone.js 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",您的问题应该会消失。

关于javascript - jshint 错误 : Redefinition of '$' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24510773/

相关文章:

javascript - 将特定的 Javascript 变量传递给 PHP

jquery - 不支持的浏览器上的 html5 必需属性

javascript - 如何在可以添加、编辑和删除的动态表上保留复选框值?

javascript - 如果我更改 Node 版本并执行 npm install,package-lock.json 中的包版本是否会更改?

javascript - 如何从url中删除同一页面的超链接名称?

mysql - 将dialogflow连接到mysql数据库以将数据插入mysql表

node.js - 如何在 Node js 中创建 ElasticSearch 快照?

node.js - Windows上的多个版本的 Node

javascript - 使用新值刷新 dat.gui

javascript - 添加代码智能到nodejs中的rest api链接