阅读.npmrc文件中的代理变量,但它不起作用。尽量避免手动下载所有需要的包并安装。
当前回答
我是这样解决这个问题的:
我运行这个命令: NPM配置strict-ssl为false 然后设置npm运行http,而不是https: NPM配置注册表http://registry.npmjs.org/ 然后我使用下面的语法安装包: NPM——proxy http://username:password@cacheaddress.com.br:80安装packagename
如果代理不要求您进行身份验证,则跳过用户名:密码部分
编辑:我的一个朋友刚刚指出,你可以通过设置HTTP_PROXY和HTTPS_PROXY环境变量来让NPM在代理后工作,然后正常地发出命令NPM install express(例如)
EDIT2:正如@ bstruthers评论的那样,请记住,包含“@”的密码不会被正确解析,如果包含@,则将整个密码放在引号中
其他回答
我尝试了所有这些选择,但出于某种原因,我的代理人没有任何选择。然后,出于绝望,我在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中设置代理了,但是对于那个会话,它是可以工作的。
我只是有我的份额与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中被修复。
对我来说,尽管python等都可以工作,但我们的公司代理npm不会。
我试着
NPM配置设置代理http://proxyccc.xxx.ca:8080 NPM配置http -proxy https://proxyccc.xxx.ca:8080 NPM配置设置注册表http://registry.npmjs.org/
按照指示,但总是得到相同的错误。
只是当我移开的时候 https-proxy https://proxyccc.xxx.ca: 8080 从.npmrc文件 那 NPM安装电子——save-dev工作正常
$ npm config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
推荐文章
- npm start和npm run start的区别
- 有没有办法修复包锁。json lockfileVersion所以npm使用特定的格式?
- 如何使用npm全局安装一个模块?
- 实时http流到HTML5视频客户端的最佳方法
- 使用node.js下载图像
- Node.js Express中的HTTP GET请求
- Node.js:将文本文件读入数组。(每一行都是数组中的一项。)
- npm犯错!错误:EPERM:操作不允许,重命名
- Node Sass还不支持当前环境:Linux 64位,带false
- 我如何添加环境变量启动。VSCode中的json
- 解析错误:无法读取文件“…/tsconfig.json”.eslint
- 编译typescript时'tsc命令未找到'
- 在Node.js中'use strict'语句是如何解释的?
- 当WebSockets可用时,为什么要使用AJAX ?
- 使用过程。TypeScript中的env