我这样解决了这个问题:
我运行这个命令: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 安装包名
如果代理不需要您进行身份验证,请跳过 username:password
部分
编辑:我的一个朋友刚刚指出,您可以通过设置 BOTH HTTP_PROXY 和 HTTPS_PROXY 环境变量来让 NPM 在代理后面工作,然后正常发出命令 npm install express (例如)
EDIT2:正如@BStruthers 评论的那样,请记住包含“@”的密码不会被正确解析,如果包含@,则将整个密码放在引号中
设置 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 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 文件的底部:##################### # 用户变量(编辑这些!)#### ################## username="myusername" password="mypassword" proxy="mycompany:8080" ############### ####### # 环境变量#(npm 确实使用这些变量,它们对许多应用程序至关重要)##################### 导出HTTPS_PROXY="http://$username:$password@$proxy" 导出 HTTP_PROXY="http://$username:$password@$proxy" 导出 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 设置 ########## ############ npm config 设置注册表 http://registry.npmjs.org/ npm conf g 设置代理 "http://$username:$password@$proxy" npm config 设置 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 设置#(奖励设置! npm 工作不需要,但许多其他程序需要)##################### 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 #(奖励设置! npm 工作不需要,但许多其他程序需要)##################### 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 安装__。如果您希望模块全局可用,请添加选项 -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 包。谢谢
您是否尝试过命令行选项而不是 .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
希望这会为某人节省时间
这在 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 代理,请设置 -g 标志:
sudo npm config set proxy http://proxy_host:port -g
对于 https 代理,再次确保设置了 -g 标志:
sudo npm config set https-proxy http://proxy_host:port -g
这对我有用-
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 config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
最后,我设法通过 AD 身份验证解决了这个问题。我不得不执行:
npm config set proxy http://domain%5Cuser:password@proxy:port/
npm config set https-proxy http://domain%5Cuser:password@proxy:port/
对任何特殊字符进行 URL 编码非常重要,例如反斜杠或 # 在我的情况下,我必须编码
使用 %5C 反斜杠,因此域\用户将是域%5Cuser # 使用 %23%0A 签名,因此 Password#2 之类的密码将是 Password%23%0A2
我还添加了以下设置:
npm config set strict-ssl false
npm config set registry http://registry.npmjs.org/
虽然我用配置设置了代理,但问题没有解决,但在这对我有用之后:
npm --https-proxy http://XX.AA.AA.BB:8080 安装cordova-plugins npm --proxy http://XX.AA.AA.BB:8080 安装
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://..
我尝试了所有这些选项,但由于某种原因,我的代理没有任何选项。然后,出于绝望/绝望,我在我的 Git Bash shell 中随机尝试了 curl
,它成功了。
使用取消设置所有代理选项
npm config rm proxy
npm config rm https-proxy
然后在我的 Git Bash shell 中运行 npm install
效果很好。我不知道它是如何为代理正确设置的,并且 Windows cmd
提示不是,但它有效。
在 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
似乎一切正常
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
在 SSL and certificate issues 上的 curl 页面上有很好的信息。我的大部分答案都是基于那里的信息。
使用 strict-ssl false 是不好的做法,可能会产生问题。我们可以做的是通过“中间人”证书添加正在注入的证书。
如何在 Windows 上解决此问题:
根据 Mozilla 的 CA 包从 curl 下载 CA 证书。您还可以使用 curl 的“firefox-db2pem.sh”shell 脚本来转换本地 Firefox 数据库。使用 https 访问网页,例如 Chrome 或 Internet Explorer 中的 Stackoverflow 单击锁定图标,单击 Chrome 中的查看证书或“有效”导航到证书路径。顶级证书或根证书是我们要提取的证书。单击该证书,然后单击“查看证书”单击第二个选项卡“详细信息”。单击“复制到文件”。选择 DER 格式并记下保存文件的位置。选择一个合适的文件名,比如 rootcert.cer 如果你安装了 Git,你将拥有 openssl.exe。否则,请在此阶段安装 git for windows。 openssl 可执行文件很可能位于 C:\Program Files\git\usr\bin\openssl.exe。我们将使用 openssl 将文件转换为 NPM 理解它所需的 PEM 格式。使用以下命令转换您在步骤 5 中保存的文件: openssl x509 -inform DES -in **rootcert**.cer -out outcert.pem -text 其中 rootcert 是您在步骤 5 中保存的证书的文件名。打开文本编辑器中的 outcert.pem 足够聪明,可以理解行尾,所以不是记事本。找到 -----BEGIN CERTIFICATE----- 很多字符 -----END CERTIFICATE----- 并复制它们之间的所有文本,包括 BEGIN / END 行现在我们将该内容粘贴到在步骤 1 中制作的 CA Cert 包的末尾。因此,在您的高级文本编辑器中打开 cacert.pem。转到文件末尾并将上一步中的内容粘贴到文件末尾。 (保留刚刚粘贴的下面的空行)将保存的 cabundle.pem 复制到合适的位置。例如你的 %userprofile% 或 ~。记下文件的位置。现在我们将告诉 npm/yarn 使用新的包。在命令行中,编写 npm config set cafile **C:\Users\username\cacert.pem** 其中 C:\Users\username\cacert.pem 是步骤 10 中的路径。可选:再次打开 strict-ssl, npm 配置设置严格-ssl true
呸!我们成功了!现在 npm 可以理解如何连接。额外的好处是你可以告诉 curl 使用相同的 cabundle.pem,它也会理解 HTTPs。
echo | openssl s_client -showcerts -servername www.domain.com -connect www.domain.com:443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > cacert.crt
先获取 crt
文件;然后通过:openssl x509 -inform PEM -in cacert.crt -out cert.pem -text
; 生成 pem 文件;并通过以下方式将 pem 文件添加到 npm cafile 中:npm config set cafile /path/to/cert.perm
这对我有用。设置 http 和 https 代理。
npm config 设置代理 http://proxy.company.com:8080
npm 配置设置 https-proxy http://proxy.company.com:8080
对我来说,即使 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 才起作用
https-proxy
可能不是 https:
。至少,为每个端口设置相同的端口可能是不正确的,但我认为它们可能具有相同的值。
就我而言,我忘记在我的配置文件中设置“http://”(可以在 C:\Users\[USERNAME]\.npmrc 中找到)代理地址。所以而不是拥有
proxy=http://[IPADDRESS]:[PORTNUMBER]
https-proxy=http://[IPADDRESS]:[PORTNUMBER]
我有
proxy=[IPADDRESS]:[PORTNUMBER]
https-proxy=[IPADDRESS]:[PORTNUMBER]
哪个当然不起作用,但错误消息也没有太大帮助......
在 cmd 或 GIT Bash 或其他提示符下使用以下命令
npm 配置设置代理“http://192.168.1.101:4128”
$ npm 配置设置 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:请删除“<”和“>”!
许多应用程序(例如 npm)可以使用来自用户环境变量的代理设置。
您只需将变量 HTTP_PROXY 和 HTTPS_PROXY 添加到您的环境中,这两个变量将具有相同的值
http://user:password@proxyAddress:proxyPort
例如,如果您有 Windows,您可以按如下方式添加代理:
https://i.stack.imgur.com/lYk1X.png
这个问题上面有很多答案,但没有一个对我有用。他们都提到要添加 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\
现在你应该准备好了!
只需打开新终端并输入 npm config edit
和 npm config -g edit
。重置为默认值。在关闭终端之后,打开新终端并键入 npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install <package>
,如果您需要全局,只需添加 -g
。
它对我有用,希望它对你有用:)
当我在代理设置中没有 http/http 前缀时,即使代理主机和端口是正确的值,npm 也会失败。它仅在添加协议前缀后才起作用。
我的问题归结为我的一个愚蠢的错误。由于我很快将代理放入 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_PROXY 和 HTTPS_PROXY
它也将解决企业环境的代理问题
我只是与 npm 和代理设置进行了斗争,因为我不喜欢其他答案,所以我想分享我认为应该如何解决这个问题(妥协安全性不是一种选择)。
什么文档说
首先,您必须了解与代理相关的 npm
可用的重要设置有哪些:
代理 用于传出 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 中修复。
my@password
,那么您的 .npmrc 文件应该有my%40password
作为密码部分。在某些情况下,将其放在引号中是可行的,但对其进行编码是万无一失的。