Code block

Google Code Prettify + AD Sense

2018年7月26日 星期四

npm使用Proxy

公司使用的TFS Build Agent因應開發者的需求安裝了Nodejs,而Nodejs的npm指令與NuGet一樣,會透過網路下載套件,不意外的,位於封閉網路中的上網問題又再次發生了

由下圖可以看到,畫面上顯示的「ETIMEOUT」就代表連接失敗


上網找了一下,基本上npm可透過指令設定proxy的參數,指令如下:
npm config set proxy http://proxy.server:proxy_port
npm config set https-proxy http://proxy.server:proxy_port

比較特別的地方是,進行 https-proxy 設定時,設定主機的地方依舊是用「http://」,而非「https://」,如果僅依照以往經驗設定成「https://proxy.server:proxy_port」,這是會失敗的,請參考下圖


不過,若您的 npm 是由其他服務、批次作業呼叫,並且以其他獨立的帳號執行,而這個帳號不允許登入 Windows 的話,也可以採用「設定檔」的方式來調整 Proxy 這個部分

設定檔中,只要輸入以下內容後存檔即可:
proxy = http://proxy.server:proxy_port
https-proxy = http://proxy.server:proxy_port

而設定檔的路徑在參考官方文件 (npm config)後,發現設定檔也是有分Project、User、Global、built-in這些等級
至於各種等級的差異,在文件上都有說明,這邊就不再贅述,但是文件上的說明,都是以 Linux   環境為基礎,倒是 Windows   環境中,會不知道檔案放在哪裡,以下為   Windows   環境中的對應 (紅色字體為檔名)
Project:C:\Project\.npmrc
User:C:\Users\<account>\.npmrc
Global:C:\Users\<account>\AppData\Roaming\npm\etc\npmrc
built-in:C:\Program  files\nodejs\node_modules\npm\npmrc

下圖為透過指令 npm config list 所列出的設定路徑,貼上來給大家參考一下


附帶一提,Project、User等級的檔名為.npmrc,在 Windows 檔案總管中,是無法將檔案改為這種檔名,請透過文字指令模式調整:
move C:\Project\npmrc.txt C:\Project\.npmrc

1 則留言:

  1. How can slot sport builders profit from slot machine software 바카라 development? First, slot sport builders can work for themselves or gaming corporations to construct attractive slot art, mechanics, and software. According to the Slot Game Designer Salary report, a slot sport designer makes $45,940 per year within the United States. Low volatility is suitable for gamers that want to win more typically. Online on line casino slot machines use a random quantity generator to determine out} the positions of the symbols. An RNG follows an algorithm that cycles hundreds of numbers every second.

    回覆刪除