為什麼要搭建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
-
檢驗是否安裝成功
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$ verdaccio warn --- config file - /Users/chenwentao/.config/verdaccio/config.yaml //配置檔案 warn --- Plugin successfully loaded: htpasswd //儲存使用者賬戶、密碼等資訊 warn --- Plugin successfully loaded: audit warn --- 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: ./plugins web: # WebUI is enabled as default, if you want disable it, just uncomment this line # web頁面的配置 即上面的http://localhost:4873/ 預設為可訪問。title就是標題,可以修改 #enable: false title: Verdaccio auth: # 儲存使用者賬戶、密碼等資訊檔案,可以將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 packages access: $all publish: $authenticated proxy: 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' registry proxy: 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` 複製程式碼