【发布时间】:2011-11-25 10:56:53
【问题描述】:
阅读.npmrc 文件中的代理变量,但它不起作用。尽量避免手动下载所有需要的包并安装。
【问题讨论】:
阅读.npmrc 文件中的代理变量,但它不起作用。尽量避免手动下载所有需要的包并安装。
【问题讨论】:
您是否尝试过命令行选项而不是 .npmrc 文件?
我认为像 npm --proxy http://proxy-server:8080/ install {package-name} 这样的东西对我有用。
我还看到了以下内容:
npm config set proxy http://proxy-server:8080/
【讨论】:
设置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 config set registry "http://registry.npmjs.org/"。它工作:)
我这样解决了这个问题:
我运行这个命令:
npm config set strict-ssl false
然后将 npm 设置为使用 http,而不是 https:
npm config set registry "http://registry.npmjs.org/"
然后我使用以下语法安装软件包:
npm --proxy http://username:password@cacheaddress.com.br:80 install packagename
如果代理不需要您进行身份验证,请跳过 username:password 部分
编辑:我的一个朋友刚刚指出,您可以通过设置 BOTH HTTP_PROXY 和 HTTPS_PROXY 环境变量,然后正常发出命令,让 NPM 在代理后面工作npm install express(例如)
EDIT2:正如@BStruthers 评论的那样,请记住包含“@”的密码将无法正确解析,如果包含@,则将整个密码放在引号中
【讨论】:
my@password,那么您的 .npmrc 文件中的密码部分应该是 my%40password。在某些情况下,将其放在引号中是可行的,但对其进行编码是万无一失的。
如有疑问,请像我一样尝试所有这些命令:
npm config set registry http://registry.npmjs.org/
npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set strict-ssl false
set HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
set HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export http_proxy=http://myusername:mypassword@proxy.us.somecompany:8080
npm --proxy http://myusername:mypassword@proxy.us.somecompany: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
【讨论】:
npm config set registry http://registry.npmjs.org/、npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080、npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080、npm config set strict-ssl false 进行 npm 配置,然后使用 npm --proxy http://myusername:mypassword@proxy.us.somecompany:8080 --without-ssl --insecure -g install {packagename} 安装 npm 包。谢谢
$ npm config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
【讨论】:
虽然我用配置设置代理,但问题没有解决,但之后 这个对我有用:
npm --https-proxy http://XX.AA.AA.BB:8080 install cordova-plugins
npm --proxy http://XX.AA.AA.BB:8080 安装
【讨论】:
要设置 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 用于字符\。
【讨论】:
encodeURIComponent("YourP@ssword") 以获取密码的编码版本。
这对我有用。 设置 http 和 https 代理。
【讨论】:
我尝试了所有这些选项,但由于某种原因我的代理没有任何选项。然后,出于绝望/绝望,我在我的 Git Bash shell 中随机尝试了curl,它成功了。
使用
取消设置所有代理选项npm config rm proxy
npm config rm https-proxy
然后在我的 Git Bash shell 中运行 npm install 效果很好。我不知道它是如何为代理正确设置的,并且 Windows cmd 提示不是,但它有效。
【讨论】:
当我在代理设置中没有 http/http 前缀时,即使代理主机和端口是正确的值,npm 也会失败。它仅在添加协议前缀后才起作用。
【讨论】:
在 Windows 系统上
尝试删除代理和注册表设置(如果已设置)并通过命令行在命令行上设置环境变量
SET HTTP_PROXY=http://username:password@domain:port
SET HTTPS_PROXY=http://username:password@domain:port
然后尝试运行 npm install。这样,您将不会在 .npmrc 中设置代理,但对于该会话,它会起作用。
【讨论】:
SET HTTP_PROXY http://username:password@domain:port,但切换到SET HTTP_PROXY=http://username:password@domain:port 似乎一切正常
在 cmd 或 GIT Bash 或其他提示符下使用以下命令
$ npm 配置设置代理“http://192.168.1.101:4128”
$ npm config set https-proxy "http://192.168.1.101:4128"
其中 192.168.1.101 是代理 ip,4128 是端口。根据您的代理设置进行更改。它对我有用。
【讨论】:
尝试在 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/
按照指示,但不断收到相同的错误。
只有当我删除
https-proxy https://proxyccc.xxx.ca:8080
从 .npmrc 文件
那
npm install electron --save-dev 工作
【讨论】:
https-proxy 可能不是https:。至少,为每个端口设置相同的端口可能是不正确的,但我认为它们可能具有相同的值。
我的问题归结为我的一个愚蠢的错误。由于我很快将代理放入 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
也许这是一个边缘案例,但希望它可以帮助某人。
【讨论】:
这对我有用-
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm set strict-ssl=false
【讨论】:
许多应用程序(例如 npm)可以使用来自用户环境变量的代理设置。
您可以将以下变量添加到您的环境中
http://user:password@proxyAddress:proxyPort
例如,如果您有 Windows,您可以按如下方式添加代理:
【讨论】:
vim ~/.npmrc 在您的 Linux 机器中并添加以下内容。不要忘记添加registry 部分,因为这在很多情况下会导致失败。
proxy=http://<proxy-url>:<port>
https-proxy=https://<proxy-url>:<port>
registry=http://registry.npmjs.org/
【讨论】:
https-proxy=https://..修改为https-proxy=http://..
在我的情况下,我忘记在我的配置文件中设置“http://”(可以在 C:\Users\[USERNAME]\.npmrc 中找到)代理地址。所以不要有
proxy=http://[IPADDRESS]:[PORTNUMBER]
https-proxy=http://[IPADDRESS]:[PORTNUMBER]
我有
proxy=[IPADDRESS]:[PORTNUMBER]
https-proxy=[IPADDRESS]:[PORTNUMBER]
这当然没有用,但错误消息也没有多大帮助......
【讨论】:
虽然已经有很多好的建议,但对于我的环境(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
希望这会为某人节省时间
【讨论】:
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:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set strict-ssl false
【讨论】:
对于这个问题,上面有很多答案,但没有一个对我有用。他们都提到要添加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
我不知道这背后的逻辑,但它奏效了。如果以上答案都不适合您,也许您可以尝试这种方式。希望这个有用。
【讨论】:
SSL and certificate issues 上的 curl 页面上有很好的信息。 我的大部分答案都是基于那里的信息。
使用 strict-ssl false 是不好的做法,可能会产生问题。我们可以做的是通过“中间人”证书添加正在注入的证书。
如何在 Windows 上解决这个问题:
openssl x509 -inform DES -in **rootcert**.cer -out outcert.pem -textnpm config set cafile **C:\Users\username\cacert.pemnpm config set strict-ssl true
呸!我们成功了!现在 npm 可以理解如何连接。好处是你可以告诉 curl 使用相同的 cabundle.pem,它也会理解 HTTPs。
【讨论】:
最后,我设法通过 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/
【讨论】:
这是我遵循的步骤(Windows):
C:\Users\<WIN_USERNAME>\.npmrc
将证书从以下地址导出到您的文件系统:https://registry.npmjs.org
导航到导出的证书位置并发出以下命令:
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
现在你应该准备好了!
【讨论】:
只需打开新终端并输入npm config edit 和npm config -g edit。重置为默认值。在关闭终端之后,打开新终端并输入npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install <package>,如果您需要全局,只需添加-g。
它对我有用,希望它对你有用:)
【讨论】:
转到环境变量并删除或将其设置为空
HTTP_PROXY 和 HTTPS_PROXY
它也将解决企业环境的代理问题
【讨论】:
我只是在与 npm 和代理设置进行斗争,因为我不喜欢其他答案,所以我想分享我认为应该如何解决这个问题(妥协安全不是一种选择)。
首先,您必须了解与代理相关的npm 可用的重要设置:
proxy 用于传出 http 请求的代理。如果设置了 HTTP_PROXY 或 http_proxy 环境变量,则底层请求库将遵循代理设置。https-proxy 用于传出 https 请求的代理。如果设置了 HTTPS_PROXY 或 https_proxy 或 HTTP_PROXY 或 http_proxy 环境变量,则底层请求库将遵循代理设置。noproxy 不应使用代理的逗号分隔字符串或域扩展数组。cafile 包含一个或多个证书颁发机构签名证书的文件的路径。类似于 ca 设置,但允许多个 CA,以及将 CA 信息存储在磁盘上的文件中。现在,由于 proxy、https-proxy 的默认值基于环境变量,因此建议正确配置这些变量,以便其他工具也可以工作(例如 curl)。
请注意,对于 v6,noproxy 文档没有说明任何有关环境变量的内容,并且提到了 since v7 NO_PROXY 环境变量。我的环境
未配置为验证此变量的工作方式(如果涵盖小写版本)。
现在我正在配置应该在代理后面使用的 docker 映像,并且 Dockerfile 中需要这些条目:
COPY certs/PoroxyCertificate.crt /usr/local/share/ca-certificates/
COPY certs/RootCa.crt /usr/local/share/ca-certificates/
RUN update-ca-certificates
# here all tools like curl were working
RUN ["/bin/bash", "-c", "set -o pipefail && curl -sSL https://deb.nodesource.com/setup_14.x | bash -"]
RUN apt-get -y update && apt-get install -y nodejs
RUN npm config set cafile /etc/ssl/certs/ca-certificates.crt -g
现在有趣的是我需要两个证书文件。 RootCa.crt 是所有公司服务器的自签名证书,PoroxyCertificate.crt 包含该证书,但它还有一个额外的中间 SubCA 证书。代理正在响应长度为 3 的证书链。
现在update-ca-certificates 扫描目录/usr/local/share/ca-certificates/ 以获取新证书并更新/etc/ssl/certs/ca-certificates.crt,其中将包含比那些自定义证书更多的内容。
将此/etc/ssl/certs/ca-certificates.crt 提供给npm config 中的cafile 可解决使用代理时证书的所有问题。
npm v6 证书错误经常导致npm ERR! Maximum call stack size exceeded 非常令人困惑(我什至故意破坏证书以验证此问题),日志文件包含如下内容:
RangeError: Maximum call stack size exceeded
at isDepOptional (/usr/lib/node_modules/npm/lib/install/deps.js:417:24)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:441:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
我找到了一些issue about that,但这不会在 v6 中修复。
【讨论】: