我现在正在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上工作?


当前回答

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

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

其他回答

或者,您可以添加到~/。NPMRC右前缀。我有C:\Program Files\nodejs的64 Win7。

以上所有的答案都不适合我。唯一有效的方法是将%AppData%\npm添加到环境Path变量中,并删除C:\Program Files\nodejs.中的两个ng文件

ng包没有安装在C:\Program Files\nodejs\ node_modules中,因此使用nodejs目录中的ng二进制文件显然是行不通的。

我不知道为什么它在这个目录中搜索,因为我已经配置了 —PATH环境变量 - C:\Users\MyUser目录下的.npmrc —尝试添加系统变量和/或NODE_PATH

我花了一段时间让全局模块工作。最后,我显式地将C:\Users\yourusername\AppData\Roaming\npm添加到“系统变量”下的PATH变量中。我还需要将这个变量放在列表中的nodejs path变量之前。

我用的是Windows 10。

如果你在windows7平台上,也许你应该像这样改变NODE_PATH: % AppData % \ npm \ node_modules

我必须添加以下路径变量下的系统变量。在用户变量下设置变量不适合我。我用的是windows 11。

%USERPROFILE%\AppData\Roaming\npm