[英]Is there a way to make npm install (the command) to work behind proxy?
閱讀.npmrc
文件中的代理變量,但它不起作用。 盡量避免手動下載所有需要的包並安裝。
我是這樣解決這個問題的:
我運行這個命令:
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 install express (例如),讓 NPM 在代理后面工作
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 代理沒有https
作為協議,而是http
。
如有疑問,請像我一樣嘗試所有這些命令:
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
您是否嘗試過命令行選項而不是.npmrc
文件?
我認為像npm --proxy http://proxy-server:8080/ install {package-name}
這樣的東西對我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
用於字符\\
。
要設置 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
雖然我用配置設置了代理,但問題沒有解決,但在這對我有用之后:
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/
最后,我設法通過 AD 身份驗證解決了這個問題。 我必須執行:
npm config set proxy http://domain%5Cuser:password@proxy:port/
npm config set https-proxy http://domain%5Cuser:password@proxy:port/
對任何特殊字符(如反斜杠或 # 在我的情況下,我必須進行編碼)進行 URL 編碼非常重要
backshlash
,因此domain\\user will
是domain%5Cuser
#
用%23%0A
簽名,所以像Password#2
這樣的Password#2
將是Password%23%0A2
我還添加了以下設置:
npm config set strict-ssl false
npm config set registry http://registry.npmjs.org/
我嘗試了所有這些選項,但由於某種原因,我的代理沒有任何選項。 然后,出於絕望/絕望,我在我的 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:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set strict-ssl false
這對我有用。 設置 http 和 https 代理。
在 Windows 系統上
嘗試刪除代理和注冊表設置(如果已經設置)並通過命令行在命令行上設置環境變量
SET HTTP_PROXY=http://username:password@domain:port
SET HTTPS_PROXY=http://username:password@domain:port
然后嘗試運行 npm install。 這樣,您將不會在 .npmrc 中設置代理,但對於該會話,它會起作用。
嘗試在 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 才起作用
就我而言,我忘記在我的配置文件(可以在 C:\\Users\\[USERNAME]\\.npmrc 中找到)代理地址中設置“http://”。 所以,而不是擁有
proxy=http://[IPADDRESS]:[PORTNUMBER]
https-proxy=http://[IPADDRESS]:[PORTNUMBER]
我有
proxy=[IPADDRESS]:[PORTNUMBER]
https-proxy=[IPADDRESS]:[PORTNUMBER]
這當然不起作用,但錯誤消息也沒有多大幫助......
curl 關於SSL 和證書問題的頁面上有很好的信息。 我的大部分答案都基於那里的信息。
使用 strict-ssl false 是不好的做法,並且會產生問題。 我們可以做的是通過“中間人”證書添加正在注入的證書。
如何在 Windows 上解決此問題:
openssl x509 -inform DES -in **rootcert**.cer -out outcert.pem -text
npm config set cafile **C:\\Users\\username\\cacert.pem
npm config set strict-ssl true
呼! 我們做到了! 現在 npm 可以理解如何連接了。 好處是你可以告訴 curl 使用相同的 cabundle.pem,它也可以理解 HTTPs。
在 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 是端口。 根據您的代理設置進行更改。 它對我有用。
許多應用程序(例如 npm)可以使用來自用戶環境變量的代理設置。
您只需將變量HTTP_PROXY和HTTPS_PROXY添加到您的環境中,這些變量對於每個變量都具有相同的值
http://user:password@proxyAddress:proxyPort
例如,如果您有 Windows,則可以按如下方式添加代理:
這個問題上面有很多答案,但沒有一個對我有用。 他們都提到要添加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
從以下地址將證書導出到您的文件系統: 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
現在你應該准備好了!
我的問題歸結為我的一個愚蠢的錯誤。 由於有一天我很快將代理放入 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 edit
和npm config -g edit
。 重置為默認值。 在關閉終端之后,打開新終端並鍵入npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install <package>
如果您需要全局添加-g
。
它對我有用,希望它對你有用:)
當我在代理設置中沒有 http/http 前綴時,即使代理主機和端口是正確的值,npm 也失敗了。 它僅在添加協議前綴后才起作用。
轉到環境變量並刪除或將其設置為空
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
文檔沒有說明任何關於環境變量的內容,並且因為提到了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
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.