Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jshint error : Redefinition of '_'

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

var _ = require('lodash')

jshint complains 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 complaining.

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

like image 473
runtimeZero Avatar asked Aug 04 '15 12:08

runtimeZero


2 Answers

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.

like image 102
musically_ut Avatar answered Nov 09 '22 12:11

musically_ut


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

like image 30
pkempenaers Avatar answered Nov 09 '22 12:11

pkempenaers