採用verdaccio搭建npm私服
前置條件
首先確保已經安裝node.js,並已配置npm。可以使用node -v 和 npm -v命令查看,正常返回版本號。
verdaccio
verdaccio簡介
Verdaccio 是一個 Node.js創建的輕量的私有npm proxy registry
它forked於[email protected]並且100% 向後兼容。
Verdaccio 表示意大利中世紀晚期fresco 繪畫中流行的一種綠色的意思。
sinopia是最初的搭建私有npm的選擇,不過已經好多年不維護了,而verdaccio則是從sinopia衍生出來並且一直在維護中的,所以現在看來,verdaccio是一個更好的選擇。
安裝
npm install -g verdaccio
//如果出現權限問題可以使用
sudo npm install -g verdaccio
查看verdaccio
輸入verdaccio回車,顯示如下信息:
lddeMacBook-Pro:verdaccio lijinwen$ verdaccio
warn — config file - /Users/lijinwen/.config/verdaccio/config.yaml
warn — Verdaccio started
warn — Plugin successfully loaded: verdaccio-htpasswd
warn — Plugin successfully loaded: verdaccio-audit
warn — http address - http://IP:4873/ - verdaccio/4.6.2
config file:配置文件,可在其中配置組件庫地址、監聽的IP和端口以及配置最大文件包限制等配置項。
http address:倉庫地址,在瀏覽器打開即可,如果還沒有在配置文件中配置,那麼默認http://localhost:4873/
配置文件實例:
#
# 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
# 所有包的緩存目錄
storage: /carlea/verdaccio/storage
# path to a directory with plugins to include
# 插件目錄
plugins: ./plugins
web:
# 管理後臺網站標題
title: 開利軟件
# comment out to disable gravatar support
# gravatar: false
# by default packages are ordercer ascendant (asc|desc)
# sort_packages: asc
# convert your UI to the dark side
# darkMode: true
# translate your registry, api i18n not available yet
# i18n:
# list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations
# web: en-US
# 驗證信息
auth:
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
# 共有倉庫配置
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
'@*/*':
# scoped packages
access: $all
publish: $authenticated
unpublish: $authenticated
# 代理 表示沒有的倉庫回去這個npmjs裏面找
# npmjs 指向https://registry.npmjs.org/,就是上面的uplinks配置
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"
# 三種身份,所有人、匿名用戶、認證(登錄)用戶,關鍵字:$all、$anonymous、$authenticated
# 是否可以訪問所需要的權限
access: $all
# allow all known users to publish/publish packages
# (anyone can register by default, remember?)
# 發佈package的權限
publish: $authenticated
# 刪除package的權限
unpublish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
# 如果package不存在,就向代理的上游服務發起請求
proxy: npmjs
# You can specify HTTP/1.1 server keep alive timeout in seconds for incoming 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 enough.
server:
keepAliveTimeout: 60
# 中間件
middlewares:
# 審計
audit:
enabled: true
# log settings
logs:
- { type: stdout, format: pretty, level: http }
#- {type: file, path: verdaccio.log, level: info}
listen: 192.168.2.212:4873 # 監聽IP和端口
max_body_size: 520mb # 設置最大包體積
#experiments:
# # support for npm token command
# token: false
# This affect the web and api (not developed yet)
#i18n:
#web: en-US
nrm
關於nrm
nrm是npm的鏡像源管理工具,有時候國外資源太慢,使用這個就可以快速地在 npm 源間切換。
首先全局安裝nrm
:npm install -g nrm
由於上面是我們自己建立的npm 私有倉庫,所以我們得添加一個自己的npm 鏡像源,添加方式:
nrm add http://IP:4873
。add 接收兩個變量 鏡像源名稱 鏡像源url地址,那麼如何查看有哪些鏡像源呢
安裝
npm install -g nrm
如果出現權限問題可以使用
sudo npm install -g nrm
查看是否安裝成,輸入nrm -V,顯示版本號。
nrm -V
1.2.1
安裝yarn(非必須)
關於yarn
yarn是facebook發佈的一款取代npm的包管理工具。
yarn的特點:
- 速度超快。
Yarn 緩存了每個下載過的包,所以再次使用時無需重複下載。 同時利用並行下載以最大化資源利用率,因此安裝速度更快。- 超級安全。
在執行代碼之前,Yarn 會通過算法校驗每個安裝包的完整性。- 超級可靠。
使用詳細、簡潔的鎖文件格式和明確的安裝算法,Yarn 能夠保證在不同系統上無差異的工作。
npm install -g yarn
如果出現權限問題可以使用
sudo npm install -g yarn
查看是否安裝成功,輸入yarn -v,顯示版本號。
yarn -v
1.22.4
nrm添加源
命令如下:name爲源名稱,url爲源地址。
nrm add name url
eg:nrm add npm https://registry.npmjs.org/
查看源列表:
nrm ls
通常添加如下源:
npm — https://registry.npmjs.org/
npm — https://registry.npm.taobao.org/
yarn — https://registry.yarnpkg.com/
yarn — https://registry.npm.taobao.org/
cnpm — https://r.cnpmjs.org/
taobao — https://registry.npm.taobao.org/
nj — https://registry.nodejitsu.com/
rednpm — https://registry.mirror.cqupt.edu.cn/
npmMirror — https://skimdb.npmjs.com/registry/
deunpm — http://registry.enpmjs.org/
local ------ http://192.168.X.XXX:4873/
注:Local爲自己搭建的私服。
使用pm2啓動verdaccio
安裝pm2守護進程工具
npm install -g pm2
如果出現權限問題可以使
sudo npm install -g pm2
啓動verdaccio
使用命令 pm2 start verdaccio
,但是在windows系統下這樣是啓動不了的,因爲在windows系統下verdaccio.cmd它不是有效的,您必須直接運行Node.js命令。
所以我們應該修改啓動命令:pm2 start C:\Users\Administrator\AppData\Roaming\npm\node_modules\verdaccio\bin\verdaccio --name verdaccio
。這樣就可以正常啓動了。
pm2簡述及常用命令
簡述
pm2是一個進程管理工具,可以用它來管理你的node進程,並查看node進程的狀態,當然也支持性能監控,進程守護,負載均衡等功能
命令
1、 pm2需要全局安裝
npm install -g pm2
或sudo npm install -g pm2
2、進入項目根目錄
啓動進程/應用
pm2 start bin/www 或 pm2 start app.js
重命名進程/應用pm2 start app.js --name wb123
添加進程/應用 watchpm2 start bin/www --watch
結束進程/應用pm2 stop www
結束所有進程/應用pm2 stop all
刪除進程/應用pm2 delete www
刪除所有進程/應用pm2 delete all
列出所有進程/應用pm2 list
查看某個進程/應用具體情況pm2 describe www
查看進程/應用的資源消耗情況pm2 monit
查看pm2的日誌pm2 logs
若要查看某個進程/應用的日誌,使用pm2 logs www
重新啓動進程/應用pm2 restart www
重新啓動所有進程/應用pm2 restart all
npm操作常用命令:
切換源:
npm config set registry http://192.168.X.XXX:4873/
查看當前鏡像地址:
npm config get registry //獲取鏡像地址
添加賬號:
npm adduser --registry=http://IP:4873
發佈組件或組件庫:
npm publish (升級組件庫的時候,package.json中配置的版本號必須大於線上版本的)
刪除組件或組件庫:
npm unpublished (只能刪除發佈時長小於24小時的)