1. 程式人生 > 實用技巧 >使用verdaccio 搭建私有npm 伺服器

使用verdaccio 搭建私有npm 伺服器

參考連結

verdaccio搭建npm私服

https://zhuanlan.zhihu.com/p/103735253

verdaccio 搭建私有 npm 及其使用方法


https://www.jianshu.com/p/dcdd44f153ae

使用verdaccio 搭建私有npm 伺服器

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

verdaccio 搭建 npm 伺服器

為什麼要搭建npm 伺服器

原因:

  1. 公司內部開發的私有包,統一管理,方便開發和使用,自然也可以使用npm 的付費服務,原諒我們的窮
  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 registryhttp://localhost:4873
# 註冊使用者
npm adduser –registryhttp://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]

大致上就是如此啦.
如果有錯誤或者疑惑之處,歡迎留言交流