搭建私有npm私庫(使用verdaccio)


搭建 npm 離線服務器

為什么要搭建npm 服務器

原因:

  1. 公司內部開發的私有包,統一管理,方便開發和使用
  2. 安全性,由於公司內部開發的模塊和一些內容並不希望其他無關人員能夠看到,但是又希望內部能方便使用
  3. 加速,自己搭建npm 服務器,本身可以自帶常用package的緩存, cnpm 有一些包存在路徑問題,而npm 的速度有些感人,自建的服務器會緩存下載過的包,能節省時間

搭建方法 使用verdaccio

verdaccio 是 sinopia 開源框架的一個fork ,但是由於sinopia 兩年前就已經沒有人維護了,由於網上搜的都是sinopia,但我實際使用了一波后,坑不要太多,哭死…….. 而且由於沒人維護,所有bug ,什么的自己去看源碼解決吧, 所以果斷棄坑, 找到了這個verdaccio

安裝

使用npm 全局安裝即可

npm install –global verdaccio

運行

安裝完成后直接輸入 verdaccio 命令即可運行

verdaccio 
運行示例 
運行示例

后面的yaml 是默認的配置文件,4873端口表示默認端口,現在我們可以通過修改默認的配置文件來符合我們的需求.

verdaccio的全部配置

默認配置如下圖所示

# #號后面是注釋
# 所有包的緩存目錄
storage: ./storage
# 插件目錄
plugins: ./plugins

#開啟web 服務,能夠通過web 訪問
web:
  # WebUI is enabled as default, if you want disable it, just uncomment this line
  #enable: false
  title: Verdaccio
#驗證信息
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

    #代理 表示沒有的倉庫會去這個npmjs 里面去找 ,
    #npmjs 又指向  https://registry.npmjs.org/ ,就是上面的 uplinks 配置
    proxy: npmjs

  '**':
    # 三種身份,所有人,匿名用戶,認證(登陸)用戶
    # "$all", "$anonymous", "$authenticated"

    #是否可訪問所需要的權限
    access: $all

    #發布package 的權限
    publish: $authenticated

    # 如果package 不存在,就向代理的上游服務發起請求
    proxy: npmjs

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true
# 監聽的端口 ,重點, 不配置這個,只能本機能訪問
listen: 0.0.0.0:4873
# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: verdaccio.log, level: info}

如何使用

#當前npm 服務指向 本地 
npm set registry http://localhost:4873 
# 注冊用戶 
npm adduser –registry http://localhost:4873

按照提示輸入userName 和 password,email

輸入后就注冊完成,

#查看當前用戶,是否是注冊用戶. 
npm who am i

最后一步就是創建一個文件夾,按照npm publish 的標准格式,創建一個私有的package

# 發布包 
npm publish

這里寫圖片描述

就成功發布了一個私有的包,

就可以在其他模塊里面使用 npm install [package name] 來安裝了, 
而私有npm 里面不包含的包,例如你要安裝一個vue ,webpack 這樣的包,找不到的話,會被代理到 npm.js 官網去下載,並且會幫你緩存在 ./storage 文件夾里面. 再次下載,就能體驗飛一般的速度了,當一個小團隊使用的時候效果更佳.

自然如果你還是不喜歡npm ,想用cnpm ,那么可以去修改配置

uplinks:
  npmjs:
    url: https://registry.npmjs.org/

packages:
  '@*/*':
    #npmjs 又指向  https://registry.npmjs.org/ ,就是上面的 uplinks 配置
    proxy: npmjs

#常用的倉庫地址
  npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

由於考慮到時常會切換倉庫來源,我是用了nrm ,一個倉庫管理器,實際上就是 簡化以下命令

npm set registry [url]

轉自 https://blog.csdn.net/qq_29594393/article/details/81587989


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM