Nodejs 在 Windows 上找不到已安装的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9587665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nodejs cannot find installed module on Windows
提问by Cosmore
I am learning nodejs at the moment on Windows. Several modules are installed globally with npm.cmd, and nodejs failed to find the installed modules. Take jade for example,
我目前正在 Windows 上学习 nodejs。npm.cmd 全局安装了几个模块,nodejs 找不到安装的模块。以玉为例,
npm install jade -g
Jade is installed in directory "C:\Program Files (x86)\nodejs\node_modules", but the following code will fail with a "Cannot find module 'jade'"error,
Jade 安装在目录 中"C:\Program Files (x86)\nodejs\node_modules",但是下面的代码会失败并"Cannot find module 'jade'"报错,
var jade = require('jade');
However, the code will run successfully when jade is locally installed (without -g option in npm). I don't want to use locally-installed modules, it's a waste of disk space for me. How can I make the globally-installed modules work on Windows?
但是,在本地安装 jade 时,代码将成功运行(npm 中没有 -g 选项)。我不想使用本地安装的模块,这对我来说是浪费磁盘空间。如何使全局安装的模块在 Windows 上工作?
回答by Alexey Ivanov
Add an environment variable called NODE_PATHand set it to %USERPROFILE%\Application Data\npm\node_modules(Windows XP), %AppData%\npm\node_modules(Windows 7/8/10), or wherever npm ends up installing the modules on your Windows flavor. To be done with it once and for all, add this as a System variable in the Advanced tab of the System Properties dialog (run control.exe sysdm.cpl,System,3).
添加一个名为的环境变量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)的“高级”选项卡中。
Quick solution in Windows 7+ is to just run:
Windows 7+ 中的快速解决方案是运行:
rem for future
setx NODE_PATH %AppData%\npm\node_modules
rem for current session
set NODE_PATH=%AppData%\npm\node_modules
It's worth to mention that NODE_PATHis only used when importing modules in Node apps. When you want to use globally installed modules' binaries in your CLI you need to add it also to your PATH, but without node_modulespart (for example %AppData%\npmin Windows 7/8/10).
值得一提的是,NODE_PATH它仅在 Node 应用程序中导入模块时使用。当你想用全球的装机量模块的二进制文件在你的CLI中,您还需要添加到您的PATH,但没有node_modules部分(例如%AppData%\npm在Windows 10年7月8日)。
Old story
老故事
I'm pretty much new to node.js myself so I can be not entirely right but from my experience it's works this way:
我自己对 node.js 还很陌生,所以我可能不完全正确,但根据我的经验,它是这样工作的:
- -gis 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_modulesfolder and checks it).
- -g不是安装全局库的方法,它只是将它们放在系统路径上的一种方法,因此您可以从命令行调用它们而无需写入它们的完整路径。它很有用,例如,node app 正在转换本地文件,比如less——如果你全局安装它,你可以在任何目录中使用它。
- node.js 本身没有查看 npm 全局目录,它使用另一种算法来查找所需的文件:http: //nodejs.org/api/modules.html#modules_file_modules(基本上它扫描路径中的每个文件夹,开始来自当前的node_modules文件夹并检查它)。
See similar question for more details: How do I install a module globally using npm?
有关更多详细信息,请参阅类似问题:如何使用 npm 全局安装模块?
回答by Hyman
if you are in the windows7 platform maybe you should change the NODE_PATH like this:
%AppData%\npm\node_modules
如果您使用的是 windows7 平台,也许您应该像这样更改 NODE_PATH:
%AppData%\npm\node_modules
回答by Vitaliy Terziev
I know i can awake a zombie but i think this is still a problem, if you need global access to node modules on Windows 7 you need to add this to your global variable path:
我知道我可以唤醒僵尸,但我认为这仍然是一个问题,如果您需要全局访问 Windows 7 上的节点模块,则需要将其添加到全局变量路径中:
C:\Users\{USER}\AppData\Roaming\npm
Important: only this without the node_modulespart, took me half hour to see this.
重要提示:只有这个没有node_modules部分,我花了半个小时才看到这个。
回答by Andi-lo
For making it work on windows 10 I solved it by adding the folder %USERPROFILE%\AppData\Roaming\npmto my PATH. Having \node_modulesappended like this: %USERPROFILE%\AppData\Roaming\npm\node_modules\did not work for me.
为了使其在 Windows 10 上运行,我通过将文件夹添加%USERPROFILE%\AppData\Roaming\npm到我的 PATH 来解决它。已经\node_modules附加这样的:%USERPROFILE%\AppData\Roaming\npm\node_modules\没有对我来说有效。
回答by marko
I'll just quote from this node's blog post...
我只是引用这个节点的博客文章......
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.
一般来说,经验法则是:
- 如果您要在程序中安装要使用的东西,请使用 require('whatever'),然后在本地安装,位于项目的根目录下。
- 如果您要在 shell、命令行或其他地方安装要在 shell 中使用的东西,请全局安装它,以便它的二进制文件最终出现在您的 PATH 环境变量中。
...
当然,在某些情况下,您希望两者都做。Coffee-script 和 Express 都是具有命令行界面和库的应用程序的好例子。在这些情况下,您可以执行以下操作之一:
- 在两个地方安装它。说真的,你的磁盘空间那么短缺吗?没关系,真的。它们是很小的 JavaScript 程序。
- 全局安装它,然后 npm link coffee-script 或 npm link express (如果你在支持符号链接的平台上。)然后你只需要更新全局副本来更新所有符号链接。
回答by S?n Tr?n-Nguy?n
To make it short, use npm link jadein your app directory.
简而言之,请npm link jade在您的应用程序目录中使用。
回答by Kong Kao
Tried to add/edit environment variables and come to conclude that:
尝试添加/编辑环境变量并得出结论:
- Edit/add
User variables(of the upper box) instead ofSystem variables(of the lower part); otherwise you have to "run as administrator" to get it work. - Append
;%AppData%\npmtoPathin order to use it as a command line tool (if supported, likejshintandgrunt-cli). - Create
NODE_PATHand set it%AppData%\npm\node_modulesin order torequire('<pkg_name>')in scripts without install it in the project directory. (Butnpm linkis suggestedfor this requirement if you're working on OS withmklinksuch as Vista and newer.)
- 编辑/添加
User variables(上部框)而不是System variables(下部);否则你必须“以管理员身份运行”才能让它工作。 - 附加
;%AppData%\npm到Path以将其用作命令行工具(如果支持,例如jshint和grunt-cli)。 - 创建
NODE_PATH并设置它%AppData%\npm\node_modules以便require('<pkg_name>')在脚本中而不将其安装在项目目录中。(但如果您使用的是 Vista 和更新版本的操作系统,npm link则建议满足此要求mklink。)
Test environment:
测试环境:
- Win 7 (Ent., 64-bit, SP1), Node.js 4.2.4, npm 2.14.12
- Win 8.1 (Ent., 64-bit), Node.js 0.10.30, npm 1.4.21
- Win 7(Ent.,64 位,SP1),Node.js 4.2.4,npm 2.14.12
- Win 8.1 (Ent., 64-bit), Node.js 0.10.30, npm 1.4.21
回答by John Galt
I had a terrible time getting global modules to work. Eventually, I explicitly added C:\Users\yourusername\AppData\Roaming\npmto the PATH variable under System Variables. I also needed to have this variable come before the nodejs path variable in the list.
我在让全局模块工作时度过了一段糟糕的时光。最后,我明确地添加C:\Users\yourusername\AppData\Roaming\npm到系统变量下的 PATH 变量。我还需要将此变量放在列表中的 nodejs 路径变量之前。
I am running Windows 10.
我正在运行 Windows 10。
回答by SCBuergel.eth
I ran into this issue on Windows 7, running
我在 Windows 7 上遇到了这个问题,运行
npm install -g gulp
as administrator while being logged on as a normal user.
以管理员身份登录,同时以普通用户身份登录。
Solution:When executing the same installation as normal user (not "run as admin" for cmd) all was fine. I guess it is related to the default install and search path.
解决方案:当执行与普通用户相同的安装(对于 cmd 不是“以管理员身份运行”)时,一切都很好。我猜这与默认安装和搜索路径有关。
回答by Kiki.J.Hu
For windows, everybody said you should set environment variables for nodejs and npm modules, but do you know why? For some modules, they have command line tool, after installed the module, there'are [module].cmdfile in C:\Program Files\nodejs, and it's used for launch in window command. So if you don't add the path containing the cmd file to environment variables %PATH%, you won't launch them successfully through command window.
对于windows,大家都说要为nodejs和npm模块设置环境变量,但是你知道为什么吗?对于某些模块,它们有命令行工具,安装模块后,在C:\Program Files\nodejs 中有[module].cmd文件,用于在窗口命令中启动。因此,如果您不将包含 cmd 文件的路径添加到环境变量%PATH%,您将无法通过命令窗口成功启动它们。

