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


当前回答

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

%USERPROFILE%\AppData\Roaming\npm

其他回答

从我使用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,每个人都说你应该为nodejs和npm模块设置环境变量,但你知道为什么吗? 对于一些模块,他们有命令行工具,安装模块后,有[模块]。cmd文件在C:\Program Files\ nodejs中,用于在窗口命令中启动。因此,如果您没有将包含cmd文件的路径添加到环境变量% path %,则无法通过命令窗口成功启动它们。

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

我将引用这个节点的博客文章…

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.

对于我来说,在Windows 10 npm配置设置前缀%AppData%\npm\node_modules