天天看點

使用 npm 私有倉庫安裝依賴遇到的坑

最近項目部署的時候時常遇到通路 npm 官方倉庫網絡抽瘋的現象,決定嘗試一下公司内部新搭建的私有倉庫(使用的 cnpm)。切換個倉庫這麼簡單的事沒想到遇到兩個大坑,記錄一下:

tarball url 指向不正确

問題:用

npm i --registry=https://rnpm.xxxxx.com xxx

指令安裝時,結果卻到

r.cnpmjs.org

這個域下面下載下傳tar包。

原因:倉庫的

registryHost

配置錯誤。

項目部署的時候大部分依賴包還是去官方倉庫下載下傳

問題:使用

npm i --registry=https://rnpm.xxxxx.com

指令安裝依賴的時候發現

registry

參數似乎不起作用,絕大多數包居然又到官方的倉庫去拉取資料(包括 metadata 和 tarball),但奇怪的是居然還有兩三個包是正常的到

rnpm.xxxxx.com

域去下載下傳資料了。

原因:因為用

npm shrinkwrap

指令鎖定了依賴,而

npm-shrinkwrap.json

裡有個

resolved

字段,這個字段指定了tarball下載下傳的位址,npm使用這個位址下載下傳了真正的依賴包。

解決辦法:目前還沒有好的解決辦法,暫時先替換掉

resolved

字段裡的 url,[email protected]之後的版本應該會提供一個優雅的解決方案。

s替換url的方法:

sed -i -- 's/https:\/\/registry\.npmjs\.org/https:\/\/rnpm\.xxxxx\.com/g' npm-shrinkwrap.json
           

其他的方案還有:shonkwrap rewrite-shrinkwrap-urls

參考:

https://github.com/npm/npm/issues/6445

https://github.com/npm/npm/issues/6324

http://blog.npmjs.org/post/145724408060/dealing-with-problematic-dependencies-in-a

https://support.sonatype.com/hc/en-us/articles/213465048-Why-does-npm-client-need-access-to-URLs-other-than-my-private-registry-

http://stackoverflow.com/questions/33682804/why-does-npm-install-use-the-shrinkwraps-resolved-property

繼續閱讀