为什么Node.js控制台中没有可用的下划线模块?

我运行下面的代码来安装下划线js模块:

npm install -g underscore 

然后我尝试通过节点控制台访问它,但我得到以下错误:

 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) 

为什么这个例子没有工作?

我不知道为什么,但确实失败了(在全局安装下划线时,就像你所做的那样)。

如果你在不使用-g的情况下安装它,它应该可以工作(不过,要小心,因为'_'已经被Node REPL用来保存最后一个操作的结果,如下所示: 在Node.js中使用Underscore模块

你真的需要全球安装吗?

我只是有同样的问题

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

为我整理; 这显然取决于你的平台和npm安装的位置。 另外,正如Javo的回答中所述,不要在REPL中将其命名为_。