阅读.npmrc文件中的代理变量,但它不起作用。尽量避免手动下载所有需要的包并安装。


当前回答

我只是有我的份额与npm和代理设置的斗争,因为我不喜欢其他的答案,我喜欢分享我认为这个问题应该如何解决(妥协安全不是一个选项)。

医生怎么说

首先,你必须了解npm中与代理相关的重要设置:

proxy A proxy to use for outgoing http requests. If the HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library. https-proxy A proxy to use for outgoing https requests. If the HTTPS_PROXY or https_proxy or HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library. noproxy A comma-separated string or an array of domain extensions that a proxy should not be used for. cafile A path to a file containing one or multiple Certificate Authority signing certificates. Similar to the ca setting, but allows for multiple CA's, as well as for the CA information to be stored in a file on disk.

现在,由于代理的默认值,http -proxy是基于环境变量的,建议正确配置这些变量,以便其他工具也可以工作(如curl)。

请注意,对于v6版本的noproxy文档,没有提到任何关于环境变量的内容,而自从v7版本开始,NO_PROXY环境变量就被提到了。我的环境中 没有配置为验证此变量如何工作(如果覆盖小写版本)。

合适的配置

现在我正在配置docker image,它应该在代理后面使用,在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

有趣的是,我需要两个证书文件。还。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配置文件解决了使用代理时证书的所有问题。

重要提示

npm v6的证书错误经常会导致npm ERR!最大调用堆栈大小超过了什么是非常令人困惑的(我甚至故意破坏证书来验证这个问题),日志文件包含如下内容:

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)

我发现了一些关于这方面的问题,但这不会在v6中被修复。

其他回答

虽然我在配置中设置了代理,但问题并没有解决 这个对我很有用:

npm——http -proxy http://XX.AA.AA.BB:8080安装cordova插件 npm——代理http://XX.AA.AA.BB:8080安装

我只是有我的份额与npm和代理设置的斗争,因为我不喜欢其他的答案,我喜欢分享我认为这个问题应该如何解决(妥协安全不是一个选项)。

医生怎么说

首先,你必须了解npm中与代理相关的重要设置:

proxy A proxy to use for outgoing http requests. If the HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library. https-proxy A proxy to use for outgoing https requests. If the HTTPS_PROXY or https_proxy or HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library. noproxy A comma-separated string or an array of domain extensions that a proxy should not be used for. cafile A path to a file containing one or multiple Certificate Authority signing certificates. Similar to the ca setting, but allows for multiple CA's, as well as for the CA information to be stored in a file on disk.

现在,由于代理的默认值,http -proxy是基于环境变量的,建议正确配置这些变量,以便其他工具也可以工作(如curl)。

请注意,对于v6版本的noproxy文档,没有提到任何关于环境变量的内容,而自从v7版本开始,NO_PROXY环境变量就被提到了。我的环境中 没有配置为验证此变量如何工作(如果覆盖小写版本)。

合适的配置

现在我正在配置docker image,它应该在代理后面使用,在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

有趣的是,我需要两个证书文件。还。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配置文件解决了使用代理时证书的所有问题。

重要提示

npm v6的证书错误经常会导致npm ERR!最大调用堆栈大小超过了什么是非常令人困惑的(我甚至故意破坏证书来验证这个问题),日志文件包含如下内容:

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)

我发现了一些关于这方面的问题,但这不会在v6中被修复。

这对我来说在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用于字符\。

以下是我所遵循的步骤(Windows):

编辑以下文件C:\Users\<WIN_USERNAME>\.npmrc . txt 将证书从以下地址导出到您的文件系统:https://registry.npmjs.org 导航到导出的证书位置并发出以下命令: NPM配置npm_certificate.cer 对文件进行如下修改: 注册表= https://registry.npmjs.org/ strict-ssl = false https-proxy = http:// [proxy_user]: [proxy_password] @ [proxy_ip]: [proxy_port] / cafile = npm_certificate.cer

现在你应该准备好了!

虽然已经有很多好的建议,但对于我的环境(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

希望这能节省别人的时间