更改 Windows 中 node.js 模块的默认全局安装目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19874582/
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
Change default global installation directory for node.js modules in Windows?
提问by gremo
In my windows installation PATHincludes C:\Program Files\nodejs, where executable node.exeis. I'm able to launch nodefrom the shell, as well as npm. I'd like new executables to be installed in C:\Program Files\nodejsas well, but it seems impossible to achieve.
在我的 Windows 安装中PATH包括C:\Program Files\nodejs,可执行文件所在的node.exe位置。我可以node从 shell启动,也可以从npm. 我也希望安装新的可执行文件C:\Program Files\nodejs,但似乎无法实现。
Setting NODE_PATHand NODE_MODULESvariables doesn't change anything: things are still installed in %appdata%\npmby default.
设置NODE_PATH和NODE_MODULES变量不会改变任何东西:%appdata%\npm默认情况下仍然安装了东西。
How can I change the global installation path?
如何更改全局安装路径?
采纳答案by ivoszz
Everything you need is to read npm-foldersdocumentation. I don't want to start my Win notebook now so I cannot verify it, but you should only change prefixto c:\Program Files\nodejsin your config file. If you want to change it globally for all users, edit c:\Program Files\nodejs\npmrcfile, otherwise create/edit c:\Users\{username}\.npmrc.
您需要做的就是阅读npm-folders文档。我现在不想启动我的 Win 笔记本,所以我无法验证它,但你应该只在你的配置文件中更改prefix为c:\Program Files\nodejs。如果要为所有用户全局更改它,请编辑c:\Program Files\nodejs\npmrc文件,否则 create/edit c:\Users\{username}\.npmrc。
But this change will have probably some side efects so read this discussionbefore. I think your idea is not a good one.
但是这种变化可能会产生一些副作用,所以请先阅读此讨论。我认为你的想法不是一个好主意。
回答by Jake Berger
trying to install globalpackages into C:\Program Files (x86)\nodejs\gave me Run as Administratorissues, because npm was trying to install intoC:\Program Files (x86)\nodejs\node_modules\
尝试将全局包安装到C:\Program Files (x86)\nodejs\我以管理员身份运行的问题中,因为 npm 试图安装到C:\Program Files (x86)\nodejs\node_modules\
to resolve this, change globalinstall directory to C:\Users\{username}\AppData\Roaming\npm:
要解决此问题,请将全局安装目录更改为C:\Users\{username}\AppData\Roaming\npm:
in C:\Users\{username}\, create .npmrcfile with contents:
在C:\Users\{username}\,创建.npmrc包含内容的文件:
prefix = "C:\\Users\\{username}\\AppData\\Roaming\\npm"
prefix = "C:\\Users\\{username}\\AppData\\Roaming\\npm"
reference
参考
npm install -g packageinstalls globalpackages into prefixlocation- npmrc userconfig takes priority and overrides
npm config ls -lwas showingprefix = "C:\\Program Files (x86)\\nodejs"
npm install -g package将全局包安装到前缀位置- npmrc userconfig 优先并覆盖
npm config ls -l正在显示prefix = "C:\\Program Files (x86)\\nodejs"
environment
nodejs x86 installer into C:\Program Files (x86)\nodejs\on Windows 7 Ultimate N 64-bit SP1node --version: v0.10.28npm --version: 1.4.10
Windows 7 Ultimate N 64 位 SP1 上的环境
nodejs x86 安装程序:v0.10.28 :1.4.10C:\Program Files (x86)\nodejs\node --versionnpm --version
回答by Tracker1
You can see my answer to this in my answer to another question.
您可以在我对另一个问题的回答中看到我对此的回答。
In Windows, the globalinstall path is actually in your user's profile directory
在 Windows 中,全局安装路径实际上在您用户的配置文件目录中
%USERPROFILE%\AppData\Roaming\npm%USERPROFILE%\AppData\Roaming\npm-cache- WARNING: If you're doing timed events or other automation as a different user, make sure you run
npm installas that user. Some modules/utilities should be installed globally. - INSTALLER BUGS: You may have to create these directories or add the
...\npmdirectory to your users path yourself.
%USERPROFILE%\AppData\Roaming\npm%USERPROFILE%\AppData\Roaming\npm-cache- 警告:如果您以其他用户身份执行定时事件或其他自动化,请确保
npm install以该用户身份运行。一些模块/实用程序应该全局安装。 - 安装程序错误:您可能需要自己创建这些目录或将
...\npm目录添加到您的用户路径中。
To change the "global" location for all users to a more appropriate shared global location %ALLUSERSPROFILE%\(npm|npm-cache)(do this as an administrator):
要将所有用户的“全局”位置更改为更合适的共享全局位置%ALLUSERSPROFILE%\(npm|npm-cache)(以管理员身份执行此操作):
- create an
[NODE_INSTALL_PATH]\etc\directory- this is needed before you try
npm config --global ...actions
- this is needed before you try
- create the global (admin) location(s) for npm modules
C:\ProgramData\npm-cache- npm modules will go hereC:\ProgramData\npm- binary scripts for globally installed modules will go hereC:\ProgramData\npm\node_modules- globally installed modules will go here- set the permissions appropriately
- administrators: modify
- authenticated users: read/execute
- Set global configuration settings (Administrator Command Prompt)
npm config --global set prefix "C:\ProgramData\npm"npm config --global set cache "C:\ProgramData\npm-cache"
- Add
C:\ProgramData\npmto your System's Path environment variable
- 创建
[NODE_INSTALL_PATH]\etc\目录- 在您尝试
npm config --global ...操作之前,这是必需的
- 在您尝试
- 为 npm 模块创建全局(管理)位置
C:\ProgramData\npm-cache- npm 模块将放在此处C:\ProgramData\npm- 全局安装模块的二进制脚本将放在此处C:\ProgramData\npm\node_modules- 全局安装的模块将放在这里- 适当设置权限
- 管理员:修改
- 经过身份验证的用户:读取/执行
- 设置全局配置设置(管理员命令提示符)
npm config --global set prefix "C:\ProgramData\npm"npm config --global set cache "C:\ProgramData\npm-cache"
- 添加
C:\ProgramData\npm到系统的 Path 环境变量
If you want to change your user's "global" location to %LOCALAPPDATA%\(npm|npm-cache)path instead:
如果要将用户的“全局”位置更改为%LOCALAPPDATA%\(npm|npm-cache)路径:
- Create the necessary directories
C:\Users\YOURNAME\AppData\Local\npm-cache- npm modules will go hereC:\Users\YOURNAME\AppData\Local\npm- binary scripts for installed modules will go hereC:\Users\YOURNAME\AppData\Local\npm\node_modules- globally installed modules will go here
- Configure npm
npm config set prefix "C:\Users\YOURNAME\AppData\Local\npm"npm config set cache "C:\Users\YOURNAME\AppData\Local\npm-cache"
- Add the new npm path to your environment's
PATH.setx PATH "%PATH%;C:\Users\YOURNAME\AppData\Local\npm"
- 创建必要的目录
C:\Users\YOURNAME\AppData\Local\npm-cache- npm 模块将放在此处C:\Users\YOURNAME\AppData\Local\npm- 已安装模块的二进制脚本将放在此处C:\Users\YOURNAME\AppData\Local\npm\node_modules- 全局安装的模块将放在这里
- 配置 npm
npm config set prefix "C:\Users\YOURNAME\AppData\Local\npm"npm config set cache "C:\Users\YOURNAME\AppData\Local\npm-cache"
- 将新的 npm 路径添加到您环境的
PATH.setx PATH "%PATH%;C:\Users\YOURNAME\AppData\Local\npm"
回答by Damilola
Building on the installation concept of chocolateyand the idea suggested by @Tracker, what worked for me was to do the following and all users on windows were then happy working with nodejsand npm.
基于chocolatey@Tracker的安装概念和建议,对我有用的是执行以下操作,然后 Windows 上的所有用户都乐于使用nodejs和npm。
Choose C:\ProgramData\nodejsas installation directory for nodejsand install nodejswith any user that is a member of the administrator group.
选择C:\ProgramData\nodejs作为管理员组成员的任何用户作为安装目录nodejs并安装nodejs。
This can be done with chocolatey as: choco install nodejs.install -ia "'INSTALLDIR=C:\ProgramData\nodejs'"
这可以用巧克力来完成: choco install nodejs.install -ia "'INSTALLDIR=C:\ProgramData\nodejs'"
Then create a folder called npm-cacheat the root of the installation directory, which after following above would be C:\ProgramData\nodejs\npm-cache.
然后npm-cache在安装目录的根目录下创建一个名为C:\ProgramData\nodejs\npm-cache.
Create a folder called etcat the root of the installation directory, which after following above would be C:\ProgramData\nodejs\etc.
etc在安装目录的根目录下创建一个名为C:\ProgramData\nodejs\etc.
Set NODEenvironment variable as C:\ProgramData\nodejs.
将NODE环境变量设置为C:\ProgramData\nodejs.
Set NODE_PATHenvironment variable as C:\ProgramData\nodejs\node_modules.
将NODE_PATH环境变量设置为 C:\ProgramData\nodejs\node_modules.
Ensure %NODE%environment variable previously created above is added (or its path) is added to %PATH%environment variable.
确保%NODE%将上面创建的环境变量(或其路径)添加到%PATH%环境变量中。
Edit %NODE_PATH%\npm\npmrcwith the following content prefix=C:\ProgramData\nodejs
%NODE_PATH%\npm\npmrc使用以下内容进行编辑prefix=C:\ProgramData\nodejs
From command prompt, set the global config like so...
在命令提示符下,像这样设置全局配置...
npm config --global set prefix "C:\ProgramData\nodejs"
npm config --global set prefix "C:\ProgramData\nodejs"
npm config --global set cache "C:\ProgramData\nodejs\npm-cache"
npm config --global set cache "C:\ProgramData\nodejs\npm-cache"
It is important the steps above are carried out preferably in sequence and before updating npm (npm -g install npm@latest) or attempting to install any npmmodule.
在更新 npm ( npm -g install npm@latest) 或尝试安装任何npm模块之前,最好按顺序执行上述步骤,这一点很重要。
Performing the above steps helped us running nodejsas system wide installation, easily available to all users with proper permissions. Each user can then run nodeand npmas required.
执行上述步骤帮助我们nodejs在系统范围内运行,所有具有适当权限的用户都可以轻松使用。然后,每个用户都可以运行node,并npm根据需要。
回答by davejoem
You should use this command to set the global installation flocation of npm packages
你应该使用这个命令来设置 npm 包的全局安装位置
(git bash)npm config --global set prefix </path/you/want/to/use>/npm
(git bash)npm config --global set prefix </path/you/want/to/use>/npm
(cmd/git-cmd)npm config --global set prefix <drive:\path\you\want\to\use>\npm
(cmd/git-cmd)npm config --global set prefix <drive:\path\you\want\to\use>\npm
You may also consider the npm-cachelocation right next to it. (as would be in a normal nodejs installation on windows)
您也可以考虑npm-cache它旁边的位置。(就像在 Windows 上的普通 nodejs 安装中一样)
(git bash)npm config --global set cache </path/you/want/to/use>/npm-cache
(git bash)npm config --global set cache </path/you/want/to/use>/npm-cache
(cmd/git-cmd)npm config --global set cache <drive:\path\you\want\to\use>\npm-cache
(cmd/git-cmd)npm config --global set cache <drive:\path\you\want\to\use>\npm-cache
回答by Kandeepa
Step 1:
npm config get prefix
- Default Path is :
%USERPROFILE%\AppData\Roaming\npm
- Default Path is :
Step 2:
npm config get cache
- Default Path is :
%USERPROFILE%\AppData\Roaming\npm-cache
- Default Path is :
Step 3:
npm config set prefix \npm
- example
npm config set prefix C:\\dev\\node\\npm
- example
Step 4:
npm config set cache \npm-cache
- example
npm config set cache C:\\dev\\node\\npm-cache
- example
第 1 步:
npm config 获取前缀
- 默认路径是:
%USERPROFILE%\AppData\Roaming\npm
- 默认路径是:
第 2 步:
npm 配置获取缓存
- 默认路径是:
%USERPROFILE%\AppData\Roaming\npm-cache
- 默认路径是:
第 3 步:
npm 配置设置前缀 \npm
- 例子
npm config set prefix C:\\dev\\node\\npm
- 例子
第 4 步:
npm 配置集缓存 \npm-cache
- 例子
npm config set cache C:\\dev\\node\\npm-cache
- 例子
Run steps 1 & 2 again to check whether the paths are updated as required
再次运行步骤 1 和 2 以检查路径是否按要求更新
That's it. Whenever you install global packages you should see them installed inside \npm\node_modules
就是这样。每当你安装全局包时,你应该看到它们安装在 \npm\node_modules 中
回答by angeldsWang
The default global folder is C:\Users\{username}\AppData\Roaming\npm.
You can create (if it doesn't exist) a .npmrcfile in C:\Users\{username}\and add
prefix = "path\\to\\yourglobalfolder".
Note that, in windows, the path should be separated by double back-slash.
默认的全局文件夹是C:\Users\{username}\AppData\Roaming\npm. 您可以在其中创建(如果它不存在)一个.npmrc文件C:\Users\{username}\并添加
prefix = "path\\to\\yourglobalfolder". 请注意,在 Windows 中,路径应以double back-slash.
回答by dynamiclynk
Using a Windows symbolic link from the C:\Users{username}\AppData\Roaming\npmand C:\Users{username}\AppData\Roaming\npm-cachepaths to the destination worked great for me.
使用从C:\Users{username}\AppData\Roaming\npm和C:\Users{username}\AppData\Roaming\npm-cache路径到目的地的 Windows 符号链接对我来说非常有用。
回答by Vinh Trieu
In Windows, if you want to move the npm or nodejs folder in disk C to another location, but it still makes sure node and npm works well, you can create symlink like this: Open Command Prompt:
在 Windows 中,如果你想将 C 盘中的 npm 或 nodejs 文件夹移动到另一个位置,但它仍然确保 node 和 npm 运行良好,你可以像这样创建符号链接:打开命令提示符:
mklink /D "your_location_want_to_create_symlink" "location_of_node_npm_file"
Example:
例子:
mklink /D "C:\Users\MyUser\AppData\Roaming\npm" "D:\Nodejs Data\npm"
Now you've created a symlink for npm folder, this symlink will refer to D:\Nodejs Data\npmEverything will work well.
现在你已经为 npm 文件夹创建了一个符号链接,这个符号链接将引用D:\Nodejs Data\npm一切都会运行良好。
回答by Manoj
I tried most of the answers here nothing seems to work in my case. So i changed the Temp location in my env variables to C:\npm. Then it started to work. This is not a good idea but a temporary solution.
我在这里尝试了大部分答案,在我的情况下似乎没有任何效果。因此,我将 env 变量中的 Temp 位置更改为 C:\npm。然后它开始工作。这不是一个好主意,而是一个临时解决方案。


