最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jshint error : Redefinition of '_' - Stack Overflow

programmeradmin0浏览0评论

I am using the lodash library in my express application. Whenever I include lodash like:

var _ = require('lodash')

jshint plains with the error:

Redefinition of '_' 

If I remove the require statement, the application fails and reports that it does not recoginice '_'.

My jshint.rc has the following statement:

 "globals": {
    "angular": false,
    "_" : false
  }

But this is so that I can include it in the front-end code without jshint plaining.

How do I ask jshint to ignore this error in my node code ?

I am using the lodash library in my express application. Whenever I include lodash like:

var _ = require('lodash')

jshint plains with the error:

Redefinition of '_' 

If I remove the require statement, the application fails and reports that it does not recoginice '_'.

My jshint.rc has the following statement:

 "globals": {
    "angular": false,
    "_" : false
  }

But this is so that I can include it in the front-end code without jshint plaining.

How do I ask jshint to ignore this error in my node code ?

Share Improve this question edited Aug 4, 2015 at 12:19 runtimeZero asked Aug 4, 2015 at 12:15 runtimeZeroruntimeZero 28.1k28 gold badges79 silver badges130 bronze badges 4
  • in node _ is a reserved word (it's the value from the last instruction evaluated) – Dario Commented Aug 4, 2015 at 12:22
  • Juhana's ment helped resolve. Basically added the following to the top of the file /* jshint -W079 */ – runtimeZero Commented Aug 4, 2015 at 12:23
  • @Juhana I'll trust you, but if I remember well in REPL was that. Am I wrong (just for don't do mistakes one more time)? – Dario Commented Aug 4, 2015 at 12:30
  • 1 @Dario REPLs are allowed to do whatever they like. But just because they assign the last return value to that variable, that doesn’t make it a reserved word. – poke Commented Aug 4, 2015 at 13:54
Add a ment  | 

2 Answers 2

Reset to default 5

You have explicitly told jshint that the global variable _ is read-only.

From the docs:

globals

A directive for telling JSHint about global variables that are defined elsewhere. If value is false (default), JSHint will consider that variable as read-only. Use it together with the undef option.

/* globals MY_LIB: false */

Since you are using require to explicitly define it, I think you can remove _ from the globals list for JSHint to allow assignment to the variable.

If, however, you are using _ without explicitly requiring it and expect it to be present in the environment, then you can set "_" : true in your .jshintrc to still allow assignment to it.

In your jshint.rc you can put: "no-native-reassign" : 0, this will disable the native reassign rule, or you can put /*jshint -W079 */ right before the function where you assign _.

When you don't use _ as a global variable you also should remove it from your Globals in jshint. Or set it to true so jshint doesn't see it as readonly.

sources: JSLint Error explanation

发布评论

评论列表(0)

  1. 暂无评论