148

I've been learning about node.js and modules, and can't seem to get the Underscore library to work properly... it seems that the first time I use a function from Underscore, it overwrites the _ object with the result of my function call. Anyone know what's going on? For example, here is a session from the node.js REPL:

Admin-MacBook-Pro:test admin$ node
> require("./underscore-min")
{ [Function]
  _: [Circular],
  VERSION: '1.1.4',
  forEach: [Function],
  each: [Function],
  map: [Function],
  inject: [Function],
  (...more functions...)
  templateSettings: { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g },
  template: [Function] }
> _.max([1,2,3])
3
> _.max([4,5,6])
TypeError: Object 3 has no method 'max'
    at [object Context]:1:3
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)
    at ReadStream.onData (tty_posix.js:70:12)
> _
3

When I make Javascript files myself and import them, they seem to be working properly. Maybe there's something special with the Underscore library?

Charles
  • 50,943
  • 13
  • 104
  • 142
Geoff
  • 9,470
  • 13
  • 52
  • 67

5 Answers5

195

As of today (April 30, 2012) you can use Underscore as usual on your Node.js code. Previous comments are right pointing that REPL interface (Node's command line mode) uses the "_" to hold the last result BUT on you are free to use it on your code files and it will work without a problem by doing the standard:

var _ = require('underscore');
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 7
    Note, this doesn't work if you try to globalize underscore: https://gist.github.com/3220108 – Lance Jul 31 '12 at 20:16
  • 9
    Someone once told me that Globals are bad on all development languages. I do not see a problem having to specify var _ = require('underscore') on the modules who require it. http://nodejs.org/api/modules.html#modules_caching –  Aug 01 '12 at 23:08
  • What Node version does 30-apri-2012 correspond with? – poseid May 14 '13 at 13:10
  • April 2012 corresponds to 0.6. –  May 28 '13 at 20:13
  • Erick, it's a problem if you're trying to reuse client-side code on the server side as well. – Brandon Aug 27 '14 at 23:44
  • This code is not correct at all, as it will override Node's own `_` value just for the immediately following operation. Example: `var _ = require('underscore');` sets `_` to the underscore library, but if you do an operation (eg: `_.isNaN(2)`) then node will use the same variable to store the result of the operation, thus a following `_.now()` will yeld a **TypeError: _.now is not a function** – GavinoGrifoni Nov 04 '15 at 08:40
169

The Node REPL uses the underscore variable to hold the result of the last operation, so it conflicts with the Underscore library's use of the same variable. Try something like this:

Admin-MacBook-Pro:test admin$ node
> _und = require("./underscore-min")
{ [Function]
  _: [Circular],
  VERSION: '1.1.4',
  forEach: [Function],
  each: [Function],
  map: [Function],
  inject: [Function],
  (...more functions...)
  templateSettings: { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g },
  template: [Function] }
> _und.max([1,2,3])
3
> _und.max([4,5,6])
6
Mike Scott
  • 4,719
  • 2
  • 21
  • 12
29

Or :

    var _ = require('underscore')._;
micrub
  • 740
  • 8
  • 17
13

The name _ used by the node.js REPL to hold the previous input. Choose another name.

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
dkiyatkin
  • 614
  • 7
  • 11
-2

Note: The following only works for the next line of code, and only due to a coincidence.

With Lodash,

require('lodash');
_.isArray([]); // true

No var _ = require('lodash') since Lodash mysteriously sets this value globally when required.

Code Whisperer
  • 22,959
  • 20
  • 67
  • 85
  • No, this won't work with lodash or anything else. It works in your example because, as stated above, node sets the result of the last statement to `_`. The result of your last statement was the lodash lib. So `_.isArray([])` will work on the _next line_, but never again. –  Sep 04 '15 at 12:49