node.js Windows 上的全局 npm 安装位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33819757/
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
Global npm install location on windows?
提问by thebjorn
I'm not 100% sure, but I believe I installed node v5 from the windows installer on both my home and office PCs.
我不是 100% 确定,但我相信我在我的家庭和办公室 PC 上从 Windows 安装程序安装了 node v5。
On my home PC global installs happen under %APPDATA%:
在我的家用 PC 上,全局安装发生在 %APPDATA% 下:
(dev) go|c:\srv> which lessc
c:\users\bjorn\appdata\roaming\npm\lessc
c:\users\bjorn\appdata\roaming\npm\lessc.cmd
while on my office PC, they go under program files:
在我的办公室 PC 上,它们位于程序文件下:
(dev) go|w:\srv> which lessc
c:\program files\nodejs\lessc
c:\program files\nodejs\lessc.cmd
I need to provide the full path to a number of these global tools to PyCharm's file watcher, and since the project file i shared it would make sense to not have global resources under a user folder.
我需要向 PyCharm 的文件观察器提供许多这些全局工具的完整路径,并且由于我共享的项目文件,因此在用户文件夹下没有全局资源是有意义的。
Why would the global installs end up in different folders, and how can I force them to a location that is common to all team members?
为什么全局安装最终会出现在不同的文件夹中,我如何将它们强制安装到所有团队成员共有的位置?
采纳答案by thebjorn
According to: https://docs.npmjs.com/files/folders
根据:https: //docs.npmjs.com/files/folders
- Local install (default): puts stuff in ./node_modules of the current package root.
- Global install (with -g): puts stuff in /usr/local or wherever node is installed.
- Install it locally if you're going to require() it.
- Install it globally if you're going to run it on the command line. -> If you need both, then install it in both places, or use npm link.
prefix Configuration
The prefix config defaults to the location where node is installed. On most systems, this is
/usr/local. On windows, this is the exact location of the node.exe binary.
- 本地安装(默认):将东西放在当前包根目录的 ./node_modules 中。
- 全局安装(使用 -g):将东西放在 /usr/local 或安装节点的任何地方。
- 如果您要 require() 它,请在本地安装它。
- 如果要在命令行上运行它,请全局安装它。-> 如果两者都需要,则在两个地方都安装它,或者使用 npm 链接。
前缀配置
前缀配置默认为安装节点的位置。在大多数系统上,这是
/usr/local. 在 Windows 上,这是 node.exe 二进制文件的确切位置。
The docs might be a little outdated, but they explain why global installs can end up in different directories:
文档可能有点过时,但它们解释了为什么全局安装会出现在不同的目录中:
(dev) go|c:\srv> npm config ls -l | grep prefix
; prefix = "C:\Program Files\nodejs" (overridden)
prefix = "C:\Users\bjorn\AppData\Roaming\npm"
Based on the other answers, it may seem like the override is now the default location on Windows, and that I may have installed my office version prior to this override being implemented.
根据其他答案,似乎覆盖现在是 Windows 上的默认位置,并且我可能在实施此覆盖之前安装了我的办公室版本。
This also suggests a solution for getting all team members to have globals stored in the same absolute path relative to their PC, i.e. (run as Administrator):
这也提出了一种解决方案,让所有团队成员将全局变量存储在相对于他们的 PC 的相同绝对路径中,即(以管理员身份运行):
mkdir %PROGRAMDATA%\npm
setx PATH "%PROGRAMDATA%\npm;%PATH%" /M
npm config set prefix %PROGRAMDATA%\npm
open a new cmd.exe window and reinstall all global packages.
打开一个新的 cmd.exe 窗口并重新安装所有全局包。
Explanation (by lineno.):
说明(由 lineno.):
- Create a folder in a sensible location to hold the globals (Microsoft is adamant that you shouldn't write to ProgramFiles, so %PROGRAMDATA% seems like the next logical place.
- The directory needs to be on the path, so use
setx .. /Mto set the system path (under HKEY_LOCAL_MACHINE). This is what requires you to run this in a shell with administrator permissions. - Tell
npmto use this new path. (Note: folder isn't visible in %PATH% in this shell, so you must open a new window).
- 在合理的位置创建一个文件夹来保存全局变量(Microsoft 坚持认为您不应写入 ProgramFiles,因此 %PROGRAMDATA% 似乎是下一个合乎逻辑的位置。
- 目录需要在路径上,所以
setx .. /M用来设置系统路径(在HKEY_LOCAL_MACHINE下)。这就是要求您在具有管理员权限的 shell 中运行它的原因。 - 告诉
npm使用这个新路径。(注意:文件夹在此 shell 的 %PATH% 中不可见,因此您必须打开一个新窗口)。
回答by Dennis Stücken
These are typical npm paths if you install a package globally:
如果您全局安装软件包,这些是典型的 npm 路径:
Windows XP - %USERPROFILE%\Application Data\npm\node_modules
Newer Windows Versions - %AppData%\npm\node_modules
or - %AppData%\roaming\npm\node_modules
回答by Subhan Luckiest
Just press windows button and type %APPDATA% and type enter.
只需按下 Windows 按钮并输入 %APPDATA% 并输入回车。
Above is the location where you can find \npm\node_modules folder. This is where global modules sit in your system.
以上是您可以找到 \npm\node_modules 文件夹的位置。这是全局模块在您的系统中的位置。
回答by java-addict301
If you're just trying to find out where npm is installing your global module (the title of this thread), look at the output when running npm install -g sample_module
如果您只是想找出 npm 在哪里安装全局模块(此线程的标题),请在运行时查看输出 npm install -g sample_module
$ npm install -g sample_module C:\Users\user\AppData\Roaming\npm\sample_module-> C:\Users\user\AppData\Roaming\npm\node_modules\sample_module\bin\sample_module.js + [email protected] updated 1 package in 2.821s
$ npm install -g sample_module C:\Users\user\AppData\Roaming\npm\sample_module-> C:\Users\user\AppData\Roaming\npm\node_modules\sample_module\bin\sample_module.js + [email protected]在 2.821 秒内更新了 1 个包

