我现在正在Windows上学习nodejs。有几个模块是通过npm全局安装的。nodejs无法找到已安装的模块。以玉为例,

npm install jade -g

Jade安装在“C:\Program Files (x86)\nodejs\node_modules”目录下,但下面的代码将失败,并提示“无法找到模块' Jade '”错误。

var jade = require('jade');

然而,当jade在本地安装时(npm中没有-g选项),代码将成功运行。我不想使用本地安装的模块,这对我来说是浪费磁盘空间。如何使全局安装的模块在Windows上工作?


当前回答

从我使用win8.1的经验来看,npm在上面安装模块 C:\Users\【用户名】\ AppData \ \ npm \ node_modules徘徊 但他却继续搜索 C:\Users\【用户名】\ node_modules。

一个简单的解决方案参考模块在应用程序的全路径:

var jsonminify = require("C:/Users/Saulius/AppData/Roaming/npm/node_modules/jsonminify");

其他回答

我无意中发现了这个问题,因为我想在windows 10的新电脑上使用node.js和visual studio 2015。我在windows 7、8和8.1上使用node.js,从来没有问题node.js找到一个模块。我使用一个遗留的node.js 0.10.39,因为我必须使用这个版本,因为串行和RFXCOM模块。

windows 10的答案是在环境变量中使用C:\Users\User\node_modules设置NODE_PATH。

尝试添加/编辑环境变量,得出结论:

Edit/add User variables (of the upper box) instead of System variables (of the lower part); otherwise you have to "run as administrator" to get it work. Append ;%AppData%\npm to Path in order to use it as a command line tool (if supported, like jshint and grunt-cli). Create NODE_PATH and set it %AppData%\npm\node_modules in order to require('<pkg_name>') in scripts without install it in the project directory. (But npm link is suggested for this requirement if you're working on OS with mklink such as Vista and newer.)

测试环境:

赢7 (Ent。, 64位,SP1), Node.js 4.2.4, npm 2.14.12 win8.1 (Ent。, 64位),Node.js 0.10.30, npm 1.4.21

我也有同样的问题,试图用npm install -g bower安装bower

我认为这是因为node是由另一个用户安装的,而不是我。

我卸载了节点,然后重新安装了它。在安装过程中,我看到了Add to PATH > npm模块选项的文本:

节点安装中的消息

节点安装完成后,我再次执行了npm install -g bower。现在凉亭起作用了。

当然没有必要重新安装节点与自己的用户,像我。解决方案必须通过NODE_PATH或PATH变量,正如其他用户所解释的那样。

这只是为了说明,只有当node已经由其他用户安装(或者在安装过程中没有标记Add to PATH > npm modules选项)时,才会发生此问题。

我在Windows 7上运行时遇到了这个问题

npm install -g gulp

以管理员身份登录,同时以普通用户身份登录。

解决方案:当以普通用户执行相同的安装时(而不是“以admin身份运行”cmd),一切正常。我猜这与默认的安装和搜索路径有关。

为了让它在windows 10上工作,我通过将文件夹%USERPROFILE%\AppData\Roaming\npm添加到我的PATH来解决这个问题。像这样添加\node_modules\ USERPROFILE%\AppData\Roaming\npm\node_modules\对我不起作用。