如何創(chuàng)建私有npm倉(cāng)庫(kù)?

原文地址:如何創(chuàng)建私有npm倉(cāng)庫(kù)?

目錄

  1. 為什么要使用私有npm倉(cāng)庫(kù)呢?
  2. 如何搭建呢?
  3. 如何啟動(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

image.png

如圖:你可以直接打開(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 源間切換。
首先全局安裝nrmnpm 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呢?

  1. 首先安裝 pm2 守護(hù)進(jìn)程工具。
  2. 然后使用命令 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)了。
  3. 如果要查看pm2更多命令,請(qǐng)自行谷歌查詢(xún)。

最后推一波:
個(gè)人博客,歡迎大家前來(lái)留言。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 介紹 在上一篇(如何發(fā)布代碼到npm官方倉(cāng)庫(kù))文章中,我們介紹了如何將自己的代碼發(fā)布到npm官方倉(cāng)庫(kù),通過(guò)那種方式...
    langkee閱讀 1,706評(píng)論 0 0
  • 大體搭建步驟轉(zhuǎn)自: npm私有倉(cāng)庫(kù)搭建 而大部分截圖來(lái)自親測(cè)操作過(guò)程 一、首先需要安裝node環(huán)境 1、下載,ht...
    前端菜籃子閱讀 10,937評(píng)論 0 5
  • 大家好,我是小L,說(shuō)實(shí)話,npm私有庫(kù)不是一個(gè)新鮮名詞了,但這個(gè)概念我是第一次接觸的。以下內(nèi)容為本人半手打半復(fù)制,...
    不了LZ閱讀 1,483評(píng)論 1 2
  • 好處: 1、平時(shí)在項(xiàng)目工作中可能會(huì)用到很多通用性的代碼,比如,框架類(lèi)、工具類(lèi)以及公用的業(yè)務(wù)邏輯代碼(支付、播放器)...
    李世鏗閱讀 2,615評(píng)論 1 0
  • 寒濕,是一種疾病。中醫(yī)認(rèn)為寒濕包括外感寒濕和內(nèi)生寒濕兩個(gè)方面。 外感寒濕:外感寒濕邪氣,氣血運(yùn)行受阻,以關(guān)節(jié)、筋骨...
    2c5398fc5348閱讀 299評(píng)論 1 1

友情鏈接更多精彩內(nèi)容