我现在正在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上工作?
添加一个名为NODE_PATH的环境变量,并将其设置为%USERPROFILE%\Application Data\npm\node_modules (Windows XP), %AppData%\npm\node_modules (Windows 7/8/10),或者npm最终在Windows版本上安装模块的任何地方。为了一劳永逸地完成它,在系统属性对话框的高级选项卡中添加这个系统变量(运行control.exe sysdm.cpl,System,3)。
Windows 7+的快速解决方案是运行:
rem for future
setx NODE_PATH %AppData%\npm\node_modules
rem for current session
set NODE_PATH=%AppData%\npm\node_modules
值得一提的是,NODE_PATH只在导入Node应用中的模块时使用。当你想在你的CLI中使用全局安装模块的二进制文件时,你需要将它也添加到你的PATH中,但不需要node_modules部分(例如Windows 7/8/10中的%AppData%\npm)。
古老的故事
我自己对node.js很陌生,所以我可能不完全正确,但从我的经验来看,它是这样工作的:
-g is not a way to install global libraries, it's only a way to place them on system path so you can call them from command line without writing the full path to them. It is useful, for example, then node app is converting local files, like less — if you install it globally you can use it in any directory.
node.js itself didn't look at the npm global dir, it is using another algorithm to find required files: http://nodejs.org/api/modules.html#modules_file_modules (basically its scanning every folder in the path, starting from the current for node_modules folder and checks it).
更多细节请参见类似的问题:如何使用npm全局安装一个模块?
我将引用这个节点的博客文章…
In general, the rule of thumb is:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of
your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its
binaries end up in your PATH environment variable.
...
Of course, there are some cases where you want to do both.
Coffee-script and Express both are good examples of apps that have a
command line interface, as well as a library. In those cases, you can
do one of the following:
Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then
you only need to update the global copy to update all the symlinks as
well.
尝试添加/编辑环境变量,得出结论:
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选项)时,才会发生此问题。