阅读.npmrc
文件中的代理变量,但它不起作用。试图避免手动下载所有需要包和安装。
我这样解决了这个问题:
npm config set strict-ssl false
npm config set registry "http://registry.npmjs.org/"
npm --proxy http://username:[email protected]:80 install packagename
如果代理不要求您进行身份验证,请跳过username:password
部分
编辑:我的一个朋友刚刚指出你可以通过设置BOTH HTTP_PROXY和HTTPS_PROXY环境变量让NPM在代理后面工作,然后正常发出命令npm install express(例如)
EDIT2:正如@BStruthers评论的那样,请记住,如果包含@将整个密码放在引号中,那么包含“@”的密码将无法正确解析
在你的Linux机器上添加vim ~/.npmrc
并添加以下内容。不要忘记添加registry
部分,因为在许多情况下导致失败。
proxy=http://<proxy-url>:<port>
https-proxy=https://<proxy-url>:<port>
registry=http://registry.npmjs.org/
虽然我用config设置代理,但问题没有解决,但是这个之后对我有用:
npm --https-proxy http://XX.AA.AA.BB:8080安装cordova-plugins
npm --proxy http://XX.AA.AA.BB:8080安装
我尝试了所有这些选项,但由于某种原因,我的代理没有任何选项。然后,出于绝望/绝望,我随机尝试了我的Git Bash shell中的curl
,它起作用了。
使用取消设置所有代理选项
npm config rm proxy
npm config rm https-proxy
然后在我的Git Bash shell中运行npm install
工作得很好。我不知道它是如何正确设置代理和Windows cmd
提示不是,但它的工作原理。
npm config set proxy <http://...>:<port_number>
npm config set registry http://registry.npmjs.org/
这解决了我的问题。
在最终得到不同的答案之后,@ Kayvar回答了前四行帮助我解决问题:
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
最后,我设法用AD身份验证解决了代理问题。我不得不执行:
npm config set proxy http://domain%5Cuser:password@proxy:port/
npm config set https-proxy http://domain%5Cuser:password@proxy:port/
对任何特殊字符(如反斜杠或#)进行URL编码非常重要。在我的情况下,我必须编码
backshlash
与%5C所以domain\user will
是domain%5Cuser
#
与%23%0A
签署所以密码像Password#2
将是Password%23%0A2
我还添加了以下设置:
npm config set strict-ssl false
npm config set registry http://registry.npmjs.org/
这对我有用。设置http和https代理。
尝试在C:\ Users \ .npmrc中找到.npmrc
然后打开(记事本),写入并保存在里面:
proxy=http://<username>:<pass>@<proxyhost>:<port>
PS:请删除“<”和“>”!
对我来说即使python等都会工作,虽然我们的公司代理npm不会。
我试过了
npm config set proxy http://proxyccc.xxx.ca:8080
npm config set https-proxy https://proxyccc.xxx.ca:8080
npm config set registry http://registry.npmjs.org/
按照指示,但不断得到同样的错误。
只有当我从.npmrc文件中删除了https-proxy https://proxyccc.xxx.ca:8080
时,npm install electron --save-dev才能工作
在cmd或GIT Bash或其他提示符下使用以下命令
$ npm config set proxy“http://192.168.1.101:4128”
$ npm config set https-proxy“http://192.168.1.101:4128”
其中192.168.1.101是代理IP,4128是端口。根据您的代理设置进行更改。它适合我。
设置npm
代理
对于HTTP
:
npm config set proxy http://proxy_host:port
对于HTTPS
:
使用https代理地址(如果有)
npm config set https-proxy https://proxy.company.com:8080
否则重用http代理地址
npm config set https-proxy http://proxy.company.com:8080
注意:https-proxy没有https
作为协议,但是http
。
许多应用程序(例如npm)可以使用来自用户环境变量的代理设置。
您可以在变量HTTP_PROXY和HTTPS_PROXY之后添加到您的环境中,每个变量具有相同的值
http://user:password@proxyAddress:proxyPort
例如,如果您有Windows,则可以添加代理,如下所示:
关于SSL and certificate issues的curl页面有很好的信息。我的大部分答案都是基于那里的信息。
使用strict-ssl false是不好的做法,可能会产生问题。我们可以做的是添加由“中间人”证书注入的证书。
如何在Windows上解决这个问题:
openssl x509 -inform DES -in **rootcert**.cer -out outcert.pem -text
其中rootcert是您在步骤5中保存的证书的文件名。npm config set cafile **C:\Users\username\cacert.pem
其中C:\ Users \ username \ cacert.pem是步骤10的路径。npm config set strict-ssl true
唷!我们做到了!现在npm可以理解如何连接。奖金是你可以告诉curl使用相同的cabundle.pem,它也会理解HTTP。
在Windows系统上
尝试删除代理和注册表设置(如果已设置)并在命令行上设置环境变量
SET HTTP_PROXY=http://username:password@domain:port
SET HTTPS_PROXY=http://username:password@domain:port
然后尝试运行npm install。这样,您就不会在.npmrc中设置代理,但是对于该会话,它将起作用。
在我的情况下,我忘了在我的配置文件中设置“http://”(可以在C:\ Users \ [USERNAME] \ .npmrc中找到)代理地址。所以没有
proxy=http://[IPADDRESS]:[PORTNUMBER]
https-proxy=http://[IPADDRESS]:[PORTNUMBER]
我有
proxy=[IPADDRESS]:[PORTNUMBER]
https-proxy=[IPADDRESS]:[PORTNUMBER]
这当然没有用,但错误信息也没有多大帮助......
这个问题上面有很多答案,但没有一个对我有用。所有这些都提到添加http://
前缀。所以我也添加了它。都失败了。
在我意外删除了http://
前缀之后它终于起作用了。最终配置是这样的:
npm config set registry http://registry.npmjs.org/
npm config set http-proxy ip:port
npm config set https-proxy ip:port
npm config set proxy ip:port
npm set strict-ssl false
我不知道这背后的逻辑,但它奏效了。如果上述答案都不适合您,也许您可以尝试这种方式。希望这个有用。
以下是我遵循的步骤(Windows):
C:\Users\<WIN_USERNAME>\.npmrc
npm config set cafile npm_certificate.cer
registry=https://registry.npmjs.org/
strict-ssl=false
https-proxy=http://[proxy_user]:[proxy_password]@[proxy_ip]:[proxy_port]/
cafile=npm_certificate.cer
现在你应该准备好了!
我的问题归结为我的一个愚蠢的错误。因为我有一天很快将我的代理放入windows * .bat文件(http_proxy,https_proxy和ftp_proxy),我忘了逃避url编码的域\用户(%5C)的特殊字符和带有问号的密码'?' (%3F)。也就是说,一旦你有编码的命令,不要忘记转义bat文件命令中的'%'。
我变了
set http_proxy=http://domain%5Cuser:password%3F@myproxy:8080
至
set http_proxy=http://domain%%5Cuser:password%%3F@myproxy:8080
也许这是一个边缘案例,但希望它可以帮助某人。
当我在代理设置中没有http / http前缀时,npm失败,即使代理主机和端口是正确的值。它只在添加协议前缀后才起作用。
只需打开新终端并输入npm config edit
和npm config -g edit
。重置为默认值。在关闭终端之后,打开新的终端并键入npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install <package>
如果你需要全局只需添加-g
。
它对我有用,希望它对你有用:)
如有疑问,请像我一样尝试所有这些命令:
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
=======
将您的设置放入~/.bashrc
或~/.bash_profile
,这样您每次打开新的终端窗口时都不必担心设置!
如果你的公司和我的公司一样,我必须经常更改我的密码。所以我将以下内容添加到〜/ .bashrc或〜/ .bash_profile中,以便每当我打开终端时,我都知道我的npm是最新的!
~/.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
您是否尝试过命令行选项而不是.npmrc
文件?
我觉得像npm --proxy http://proxy-server:8080/ install {package-name}
这样的东西对我有用。
我也看到了以下内容:npm config set proxy http://proxy-server:8080/
虽然已经有很多好的建议,但对于我的环境(Windows 7,使用PowerShell)和node.js(v8.1.2)的最新版本,除了我遵循brunowego设置外,所有上述操作都没有用。
因此,请检查您的设置:
npm config list
代理背后的设置:
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
希望这会为某人节省时间
要设置http代理,请设置-g标志:
sudo npm config set proxy http://proxy_host:port -g
对于https代理,请再次确保设置了-g标志:
sudo npm config set https-proxy http://proxy_host:port -g
这适用于Windows:
npm config set proxy http://domain%5Cuser:pass@host:port
如果您不在任何域中,请使用:
npm config set proxy http://user:pass@host:port
如果您的密码包含特殊字符,例如"
,@
,:
等,请使用其URL编码值替换它们。例如"
-> %22
,@
-> %40
,:
-> %3A
。 %5C
用于角色\
。
$ npm config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
这对我有用 -
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm set strict-ssl=false