原文地址:如何創(chuàng)建私有npm倉(cāng)庫(kù)?
目錄
- 為什么要使用私有npm倉(cāng)庫(kù)呢?
- 如何搭建呢?
- 如何啟動(dòng)verdaccio呢?
為什么要使用私有npm倉(cāng)庫(kù)呢
隨著公司業(yè)務(wù)增加,項(xiàng)目變多,總會(huì)有很多復(fù)用的業(yè)務(wù)組件,存在不同項(xiàng)目,所以這個(gè)時(shí)候就要想如何將這些組件,發(fā)布到npm倉(cāng)庫(kù)上,讓每個(gè)項(xiàng)目組的人,通過(guò)npm install xxx 直接下載依賴(lài)呢。but,又考慮到npm倉(cāng)庫(kù)是對(duì)外的,所以自己內(nèi)部的業(yè)務(wù)組件部署上去不太好,那么就誕生出來(lái)了一個(gè)概念,就是npm私有倉(cāng)庫(kù)。其作用不僅僅是放一些業(yè)務(wù)組件,你也可以放一些自己的寫(xiě)的庫(kù)或者公司內(nèi)部使用的庫(kù)等!
如何搭建呢?
搭建npm私有倉(cāng)庫(kù)方法有多種,這里只介紹一種較為簡(jiǎn)單的方式,就是采用verdaccio
什么是verdaccio?
Verdaccio 是一個(gè) Node.js創(chuàng)建的輕量的私有npm proxy registry
它forked于sinopia@1.4.0并且100% 向后兼容。
Verdaccio 表示意大利中世紀(jì)晚期fresco 繪畫(huà)中流行的一種綠色的意思。
sinopia是最初的搭建私有npm的選擇,不過(guò)已經(jīng)好多年不維護(hù)了,而verdaccio則是從sinopia衍生出來(lái)并且一直在維護(hù)中的,所以現(xiàn)在看來(lái),verdaccio是一個(gè)更好的選擇。
首先全局安裝verdaccio:
npm install -g verdaccio
安裝完成后: cmd 輸入命令: verdaccio

如圖:你可以直接打開(kāi)http address地址,這就是你的倉(cāng)庫(kù)
接下來(lái)你要注意的一個(gè)文件就是
C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml這個(gè)文件是主要的配置文件,里面的內(nèi)容是:
#
# 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: D:\verdaccio\storage (緩存路徑,最好找一個(gè)空間比較大的盤(pán)來(lái)放,默認(rèn)是在C 盤(pán))
# path to a directory with plugins to include
plugins: ./plugins
web:
title: Verdaccio (網(wǎng)站title, 可以隨意命名)
# comment out to disable gravatar support
# gravatar: false
# by default packages are ordercer ascendant (asc|desc)
# sort_packages: asc
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.npm.taobao.org/ (鏡像源配置,可以添加多個(gè))
packages:
'@*/*':
# scoped packages
access: $all
publish: $authenticated
unpublish: $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/publish packages
# (anyone can register by default, remember?)
publish: $authenticated
unpublish: $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 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: http://IP:4873 (需要監(jiān)聽(tīng)的端口,IP)
max_body_size: 300mb (最大的文件包限制)
#experiments:
# # support for npm token command
# token: false
配置完成后,打開(kāi)網(wǎng)站http://IP:4873:
頁(yè)面會(huì)提示你去添加賬號(hào)和發(fā)布。BUT,這一步之前你可以先去設(shè)置鏡像源,這里就會(huì)涉及到一個(gè)工具叫nrm
什么是nrm呢?
nrm是npm的鏡像源管理工具,有時(shí)候國(guó)外資源太慢,使用這個(gè)就可以快速地在 npm 源間切換。
首先全局安裝nrm:npm install -g nrm
由于上面是我們自己建立的npm 私有倉(cāng)庫(kù),所以我們得添加一個(gè)自己的npm 鏡像源,添加方式:
nrm add <name> http://IP:4873。add 接收兩個(gè)變量 <name>鏡像源名稱(chēng) <url> 鏡像源url地址,那么如何查看有哪些鏡像源呢
nrm ls:
npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
taobao ----- https://registry.npm.taobao.org/
nj --------- https://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http://registry.enpmjs.org/
* xxx http://IP:4873/
列表內(nèi)已經(jīng)添加好了自己的npm鏡像源,那么關(guān)鍵的一步是切換到添加的鏡像源:nrm use xxx,更多nrm命令,請(qǐng)自行谷歌查詢(xún)。到這里,我們就可以回到我們配置verdaccio步驟去添加賬號(hào)和發(fā)布你的第一個(gè)庫(kù)了。
添加賬號(hào):npm adduser --registry=http://IP:4873
發(fā)布庫(kù)或組件:npm publish
更多npm命令,請(qǐng)查看https://cloud.tencent.com/developer/section/1490273
這樣你就可以玩轉(zhuǎn)私有npm倉(cāng)庫(kù)了!
如何啟動(dòng)verdaccio呢?
- 首先安裝 pm2 守護(hù)進(jìn)程工具。
- 然后使用命令
pm2 start verdaccio,but , 在windows系統(tǒng)下這樣是啟動(dòng)不了的,因?yàn)樵趙indows系統(tǒng)下verdaccio.cmd它不是有效的,您必須直接運(yùn)行Node.js命令。
所以我們應(yīng)該修改啟動(dòng)命令:pm2 start C:\Users\Administrator\AppData\Roaming\npm\node_modules\verdaccio\bin\verdaccio --name verdaccio。這樣就可以正常啟動(dòng)了。 - 如果要查看pm2更多命令,請(qǐng)自行谷歌查詢(xún)。
最后推一波:
個(gè)人博客,歡迎大家前來(lái)留言。