Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the underscore module available in the Node.js console?

I ran the following code to install the underscore js module:

npm install -g underscore

I then tried to access it via the node console, but I get the following error:

node
> __ = require('underscore');
Error: Cannot find module 'underscore'
  at Function.Module._resolveFilename (module.js:338:15)
  at Function.Module._load (module.js:280:25)
  at Module.require (module.js:362:17)
  at require (module.js:378:17)
  at repl:1:6
  at REPLServer.self.eval (repl.js:109:21)
  at rli.on.self.bufferedCmd (repl.js:258:20)
  at REPLServer.self.eval (repl.js:116:5)
  at Interface.<anonymous> (repl.js:248:12)
  at Interface.EventEmitter.emit (events.js:96:17)

Why doesn't this example work?

like image 359
GSto Avatar asked Apr 02 '13 16:04

GSto


2 Answers

I don't really know why, but it fails indeed (when installing underscore globally, as you have done).

If you install it without -g, it should work (be careful, however, as '_' is already used by Node REPL to hold the result of the last operation, as explained here: Using the Underscore module with Node.js

Do you really need to install it globally?

like image 153
Javo Avatar answered Nov 16 '22 17:11

Javo


I just had the same problem

$ export NODE_PATH=/usr/local/share/npm/lib/node_modules

sorted it out for me; this obviously depends on your platform and where npm has installed it. Also, as mentioned in Javo's answer, don't name it _ in the REPL.

like image 26
Leinster Avatar answered Nov 16 '22 16:11

Leinster