node.js 有没有办法让 npm install (命令)在代理后面工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7559648/
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
Is there a way to make npm install (the command) to work behind proxy?
提问by Ben
Read about a proxy variable in a .npmrcfile but it does not work. Trying to avoid manually downloading all require packages and installing.
阅读.npmrc文件中的代理变量,但它不起作用。尽量避免手动下载所有需要的软件包并安装。
回答by Renato Gama
I solved this problem this way:
我是这样解决这个问题的:
I run this command:
npm config set strict-ssl falseThen set npm to run with http, instead of https:
npm config set registry "http://registry.npmjs.org/"Then I install packages using this syntax:
npm --proxy http://username:[email protected]:80 install packagename
我运行这个命令:
npm config set strict-ssl false然后将 npm 设置为使用 http 而不是 https 运行:
npm config set registry "http://registry.npmjs.org/"然后我使用以下语法安装软件包:
npm --proxy http://username:[email protected]:80 install packagename
Skip the username:passwordpart if proxy doesn't require you to authenticate
username:password如果代理不需要您进行身份验证,请跳过该部分
EDIT: A friend of mine just pointed out that you may get NPM to work behind a proxy by setting BOTHHTTP_PROXY and HTTPS_PROXY environment variables, then issuing normally the command npm install express(for example)
编辑:我的一个朋友刚刚指出,您可以通过设置BOTHHTTP_PROXY 和 HTTPS_PROXY 环境变量,然后正常发出命令 npm install express(例如),让 NPM 在代理后面工作
EDIT2: As @BStruthers commented, keep in mind that passwords containing "@" wont be parsed correctly, if contains @ put the entire password in quotes
EDIT2:正如@BStruthers 所评论的,请记住,包含“@”的密码将不会被正确解析,如果包含 @ 将整个密码放在引号中
回答by j.i.t.h.e.s.h
Setup npmproxy
设置npm代理
For HTTP:
对于HTTP:
npm config set proxy http://proxy_host:port
For HTTPS:
对于HTTPS:
use the https proxy address if there is one
有的话就使用https代理地址
npm config set https-proxy https://proxy.company.com:8080
else reuse the http proxy address
否则重用http代理地址
npm config set https-proxy http://proxy.company.com:8080
Note: The https-proxy doesn't have httpsas the protocol, but http.
注意:https-proxy 没有https作为协议,但是http.
回答by Katie
When in doubt, try all these commands, as I do:
如有疑问,请像我一样尝试所有这些命令:
npm config set registry http://registry.npmjs.org/
npm config set proxy http://myusername:[email protected]:8080
npm config set https-proxy http://myusername:[email protected]:8080
npm config set strict-ssl false
set HTTPS_PROXY=http://myusername:[email protected]:8080
set HTTP_PROXY=http://myusername:[email protected]:8080
export HTTPS_PROXY=http://myusername:[email protected]:8080
export HTTP_PROXY=http://myusername:[email protected]:8080
export http_proxy=http://myusername:[email protected]:8080
npm --proxy http://myusername:[email protected]:8080 \
--without-ssl --insecure -g install
=======
========
UPDATE
更新
Put your settings into ~/.bashrcor ~/.bash_profileso you don't have to worry about your settings everytime you open a new terminal window!
将您的设置放入~/.bashrcor 中,~/.bash_profile这样您每次打开新的终端窗口时都不必担心您的设置!
If your company is like mine, I have to change my password pretty often. So I added the following into my ~/.bashrc or ~/.bash_profile so that whenever I open a terminal, I know my npm is up to date!
如果您的公司和我的一样,我必须经常更改密码。所以我将以下内容添加到我的 ~/.bashrc 或 ~/.bash_profile 中,这样每当我打开终端时,我就知道我的 npm 是最新的!
Simply paste the following code at the bottom of your
~/.bashrcfile:###################### # User Variables (Edit These!) ###################### username="myusername" password="mypassword" proxy="mycompany:8080" ###################### # Environement Variables # (npm does use these variables, and they are vital to lots of applications) ###################### export HTTPS_PROXY="http://$username:$password@$proxy" export HTTP_PROXY="http://$username:$password@$proxy" export http_proxy="http://$username:$password@$proxy" export https_proxy="http://$username:$password@$proxy" export all_proxy="http://$username:$password@$proxy" export ftp_proxy="http://$username:$password@$proxy" export dns_proxy="http://$username:$password@$proxy" export rsync_proxy="http://$username:$password@$proxy" export no_proxy="127.0.0.10/8, localhost, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16" ###################### # npm Settings ###################### npm config set registry http://registry.npmjs.org/ npm config set proxy "http://$username:$password@$proxy" npm config set https-proxy "http://$username:$password@$proxy" npm config set strict-ssl false echo "registry=http://registry.npmjs.org/" > ~/.npmrc echo "proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "strict-ssl=false" >> ~/.npmrc echo "http-proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "http_proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "https_proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "https-proxy=http://$username:$password@$proxy" >> ~/.npmrc ###################### # WGET SETTINGS # (Bonus Settings! Not required for npm to work, but needed for lots of other programs) ###################### echo "https_proxy = http://$username:$password@$proxy/" > ~/.wgetrc echo "http_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc echo "ftp_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc echo "use_proxy = on" >> ~/.wgetrc ###################### # CURL SETTINGS # (Bonus Settings! Not required for npm to work, but needed for lots of other programs) ###################### echo "proxy=http://$username:$password@$proxy" > ~/.curlrcThen edit the "username", "password", and "proxy" fields in the code you pasted.
Open a new terminal
Check your settings by running
npm config listandcat ~/.npmrcTry to install your module using
npm install __, ornpm --without-ssl --insecure install __, or- override your proxy settings by using
npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install __. - If you want the module to be available globally, add option
-g
只需将以下代码粘贴到
~/.bashrc文件底部:###################### # User Variables (Edit These!) ###################### username="myusername" password="mypassword" proxy="mycompany:8080" ###################### # Environement Variables # (npm does use these variables, and they are vital to lots of applications) ###################### export HTTPS_PROXY="http://$username:$password@$proxy" export HTTP_PROXY="http://$username:$password@$proxy" export http_proxy="http://$username:$password@$proxy" export https_proxy="http://$username:$password@$proxy" export all_proxy="http://$username:$password@$proxy" export ftp_proxy="http://$username:$password@$proxy" export dns_proxy="http://$username:$password@$proxy" export rsync_proxy="http://$username:$password@$proxy" export no_proxy="127.0.0.10/8, localhost, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16" ###################### # npm Settings ###################### npm config set registry http://registry.npmjs.org/ npm config set proxy "http://$username:$password@$proxy" npm config set https-proxy "http://$username:$password@$proxy" npm config set strict-ssl false echo "registry=http://registry.npmjs.org/" > ~/.npmrc echo "proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "strict-ssl=false" >> ~/.npmrc echo "http-proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "http_proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "https_proxy=http://$username:$password@$proxy" >> ~/.npmrc echo "https-proxy=http://$username:$password@$proxy" >> ~/.npmrc ###################### # WGET SETTINGS # (Bonus Settings! Not required for npm to work, but needed for lots of other programs) ###################### echo "https_proxy = http://$username:$password@$proxy/" > ~/.wgetrc echo "http_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc echo "ftp_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc echo "use_proxy = on" >> ~/.wgetrc ###################### # CURL SETTINGS # (Bonus Settings! Not required for npm to work, but needed for lots of other programs) ###################### echo "proxy=http://$username:$password@$proxy" > ~/.curlrc然后编辑您粘贴的代码中的“用户名”、“密码”和“代理”字段。
打开一个新终端
通过运行
npm config list和检查您的设置cat ~/.npmrc尝试使用安装您的模块
npm install __, 或者npm --without-ssl --insecure install __, 或者- 使用
npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install __. - 如果您希望模块全局可用,请添加选项
-g
回答by Ben Humphreys
Have you tried command-line options instead of the .npmrcfile?
您是否尝试过命令行选项而不是.npmrc文件?
I think something like npm --proxy http://proxy-server:8080/ install {package-name}worked for me.
我认为类似的东西npm --proxy http://proxy-server:8080/ install {package-name}对我有用。
I've also seen the following:
npm config set proxy http://proxy-server:8080/
我还看到了以下内容:
npm config set proxy http://proxy-server:8080/
回答by Carmine Tambascia
Though there are already many good advice, for my environment(Windows 7, using PowerShell) and the last version available of node.js ( v8.1.2 ) all the above did not worked, except when I followed brunowegosettings.
尽管已经有很多好的建议,但对于我的环境(Windows 7,使用 PowerShell)和可用的最新版本 node.js( v8.1.2 ),上述所有内容都不起作用,除非我遵循brunowego设置。
So check your settings with :
因此,请使用以下命令检查您的设置:
npm config list
Settings behind a proxy:
代理背后的设置:
npm config set registry http://registry.npmjs.org/
npm config set http-proxy http://username:password@ip:port
npm config set https-proxy http://username:password@ip:port
npm config set proxy http://username:password@ip:port
npm set strict-ssl false
Hope this will save time to someone
希望这会为某人节省时间
回答by maximus
This works for me in Windows:
这在 Windows 中对我有用:
npm config set proxy http://domain%5Cuser:pass@host:port
If you are not in any domain, use:
如果您不在任何域中,请使用:
npm config set proxy http://user:pass@host:port
If your password contains special characters such as ",@,:and so on, replace them by their URL encoded values. For example "->%22, @->%40, :->%3A. %5Cis used for the character \.
如果您的密码包含特殊字符,例如",@,:等等,通过他们的URL编码值替换它们。例如"-> %22、@-> %40、:-> %3A。%5C用于字符\。
回答by Andrei
To setup the http proxy have the -gflag set:
要设置 http 代理,请设置-g标志:
sudo npm config set proxy http://proxy_host:port -g
sudo npm config set proxy http://proxy_host:port -g
For https proxy, again make sure the -gflag is set:
对于 https 代理,再次确保设置了-g标志:
sudo npm config set https-proxy http://proxy_host:port -g
sudo npm config set https-proxy http://proxy_host:port -g
回答by Sharan Rajendran
$ npm config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
回答by abhishek khandait
This worked for me-
这对我有用-
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm set strict-ssl=false
回答by Abhishek Dwivedi
vim ~/.npmrcin your Linux machine and add following. Don't forget to add registrypart as this cause failure in many cases.
vim ~/.npmrc在您的 Linux 机器中并添加以下内容。不要忘记添加registry部分,因为这在许多情况下会导致失败。
proxy=http://<proxy-url>:<port>
https-proxy=https://<proxy-url>:<port>
registry=http://registry.npmjs.org/

