為什么要搭建npm私有庫?
- 為了方便下載時,公共包走npmjs,私有包走內部服務器。
- npm包下載的速度較慢,搭建npm私有庫之后,會先操作私有庫中是否有緩存,有緩存直接走緩存,而不用重新再去請求一遍網絡。
哪種方式適合你呢?
npm私有庫的搭建有很多種,具體哪種方式適合,我選擇的方案是比較簡單的“使用verdaccio搭建npm私有庫”。
先試著在本地搭建一個吧
-
準備工作
我們需要使用npm命令去安裝verdaccio,所以我們必須要有node環境,node環境又依賴于python。因此,在搭建npm私有庫的準備工作就是去搭建node環境。
-
檢測是否有node環境
chenwentaodeiMac:ceair_wallet chenwentao$ node -v bash: node: command not found 復制代碼
-
下載node
-
安裝node
-
檢驗是否安裝成功
chenwentaodeiMac:ceair_wallet chenwentao$ node -v v10.15.1 復制代碼
-
-
安裝啟動verdaccio
-
安裝verdaccio
安裝速度緩慢的話,可以使用淘寶鏡像,install時遇到permission denied,記得前面加sudo
chenwentaodeiMac:ceair_wallet chenwentao$ sudo cnpm install -g verdaccio 復制代碼
-
啟動verdaccio
啟動成功后,打開http://localhost:4873/,看到界面就表示成功了
chenwentaodeiMac:ceair_wallet chenwentao$ verdacciowarn --- config file - /Users/chenwentao/.config/verdaccio/config.yaml //配置文件warn --- Plugin successfully loaded: htpasswd //保存用戶賬戶、密碼等信息warn --- Plugin successfully loaded: auditwarn --- http address - http://localhost:4873/ - verdaccio/3.11.4 //地址 復制代碼
-
-
配置文件
默認的配置文件允許所有的用戶擁有任何的權限。
# # This is the default config file. It allows all users to do anything, # so don't use it on production systems. # # Look here for more config file examples: # https://github.com/verdaccio/verdaccio/tree/master/conf ## path to a directory with all packages 存儲npm包的路徑 storage: ./storage # path to a directory with plugins to include plugins: ./pluginsweb:# WebUI is enabled as default, if you want disable it, just uncomment this line # web頁面的配置 即上面的http://localhost:4873/ 默認為可訪問。title就是標題,可以修改#enable: falsetitle: Verdaccioauth: # 保存用戶賬戶、密碼等信息文件,可以將max_users設置為-1禁止用戶添加,從而通過修改htpasswd來添加用戶htpasswd:file: ./htpasswd# Maximum amount of users allowed to register, defaults to "+inf".# You can set this to -1 to disable registration.#max_users: 1000# a list of other known repositories we can talk to # 訪問公共庫的路徑,可以修改成淘寶鏡像 https://registry.npm.taobao.org uplinks:npmjs:url: https://registry.npmjs.org/packages:'@*/*':# scoped packagesaccess: $allpublish: $authenticatedproxy: npmjs'**':# 配置權限# allow all users (including non-authenticated users) to read and# publish all packages## you can specify usernames/groupnames (depending on your auth plugin)# and three keywords: "$all", "$anonymous", "$authenticated"access: $all# allow all known users to publish packages# (anyone can register by default, remember?)publish: $authenticated# if package is not available locally, proxy requests to 'npmjs' registryproxy: npmjs# You can specify HTTP/1.1 server keep alive timeout in seconds for incomming connections. # A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout. # WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enought. server:keepAliveTimeout: 60# To use `npm audit` uncomment the following section middlewares:audit:enabled: true# log settings logs:- {type: stdout, format: pretty, level: http}#- {type: file, path: verdaccio.log, level: info} # 配置之后相同wifi下其他電腦也可以訪問了 訪問地址為你的ip加上端口4873 listen: 0.0.0.0:4873復制代碼
-
客戶端配置
本地的私有倉庫已經搭建好了,接下來我們需要通過客戶端配置registry來使用我們的私有倉庫。在瀏覽器中打開http://10.68.18.154:4873/時,會有提示(10.68.18.154是本機的IP地址)
Login:
npm adduser --registry http://10.68.18.154:4873 復制代碼
Publish:
npm publish --registry http://10.68.18.154:4873 復制代碼
在linux服務器上嘗試一下
剛才,我們在本地構建了一個npm私有庫,現在我們到Linux服務器上嘗試一下吧。首先,檢測一下有沒有安裝node和python,如果沒有安裝就進行安裝,那么我們接下來來安裝一下。
-
安裝python
在Linux上安裝python,需要用命令行去操作。
下載
解壓
-
安裝node
下載
解壓
接下來和本地一樣去創建npm私有庫,創建完之后讓我們永久的運行verdaccio吧。
-
永久運行verdaccio
sudo npm install -g forever forever start `which verdaccio` 復制代碼