NPM私服搭建


前言

在工作中,我們常常會開發很多通用性代碼,比如我們之前給大家講解過的UI庫、工具類、公用業務邏輯代碼等。這些代碼我們如何發揮它的價值呢?這時可將這些庫發布到npm,需要的項目安裝使用即可,但是發布到公網npm無法保證源碼的私密性,這時我們就需要使用到私有npm倉庫。

私有npm倉庫優勢

  1. 只能在公司局域網使用,保證了代碼的私密性
  2. 因為使用局域網,依賴包下載更快
  3. 可以將發布和安裝npm的包進行權限配置,利於npm倉庫的維護
  4. 修改了第三方npm包,但是發布包的作者未將PR合並到master,導致該功能無法在安裝包后引用,這時我們可以將三方包源碼修改,發布於私有倉庫,即可下載安裝,而不用在 node_modules 中更改源碼

使用 Verdaccio

Verdaccio 是用 nodejs 開發的輕量級私有npm代理服務,因此使用 Verdaccio 前需要安裝 node 。如何安裝node不是我們這篇文章的重點,可自行搜索資料安裝node。

安裝 Verdaccio

使用 npm 安裝 Verdaccio ,需要全局安裝,所以注意權限問題。

npm install -g verdaccio

安裝完以后,執行

verdaccio -h

出現版本號相關提示則表示安裝成功。如果提示命令找不到,請重啟控制台。

運行 verdaccio

運行 verdaccio 命令很簡單,執行verdaccio即可。

verdaccio

這時,執行結果如下所示

 warn --- config file  - /root/.config/verdaccio/config.yaml
 warn --- Plugin successfully loaded: htpasswd
 warn --- Plugin successfully loaded: audit
 warn --- http address - http://localhost:4873/ - verdaccio/4.4.0

這是提示我們,verdaccio 的配置文件放在了用戶目錄下的 .config/verdaccio/config.yaml 中,編輯該文件即可修改對於 verdaccio 的配置。相關配置我們在后一段落介紹。

打開瀏覽器,輸入 localhost:4873,就能看到用於展示包的網頁,因為我們目前還沒有上傳任何包,所以該頁面為空,並且提示發布包到該倉庫。

img

配置 verdaccio

verdaccio 的配置文件為 ~/.config/verdaccio/config.yaml ,使用編輯器或者 vim 打開該文件,verdaccio 默認配置如下

#
# 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: ./storage
# path to a directory with plugins to include
plugins: ./plugins
web:
  title: Verdaccio
  # comment out to disable gravatar support
  # gravatar: false
  # by default packages are ordercer ascendant (asc|desc)
  # sort_packages: asc
auth:
    # 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
    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}
#experiments:
#  # support for npm token command
#  token: false

以下介紹重要參數的含義

storage

配置發布到私有倉庫包的存放地址,默認存放於 ~/.config/verdaccio/storage 中,我們可以定期將該文件中的內容進行清理,但是一定要謹慎,因為該文件夾中存放的包不止我們自己發布的,還有一些從公有倉庫中拉取並緩存的包(具體如何配置拉取緩存,后續參數介紹)。

也許,我們的包不止發布到了一個倉庫,如果公司按照業務線划分了幾個前端部門,部門之間技術獨立但能共享,這時如果我們想在使用自己發布的npm私有包的同時,還期望可以使用其他團隊開發的npm包,這時我們就可以通過指定該參數實現。換句話說,npm公有倉庫也能理解為我們的另一個倉庫,像這樣的倉庫還有淘寶的倉庫等。配置如下,在這里其實只是做一個定義,真正的使用其實是在包 packages 管理的參數中

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

packages

該參數是整個配置中最為重要的一個,因為通過配置該參數,能達到設定包權限,設定包發布與使用的權限,設置包是否代理到公有npm倉庫等

packages:
  '@heyi/*':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
  '**':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

通過以上參數的配置,我們就約定了,如果你發布的包是 @heyi 前綴的,那就表明是私有包,不會代理到外部。如果發布的包沒有 @heyi 前綴,則會走 ** 的邏輯,即所有不包含 @heyi 前綴的包,不難看出,proxy: npmjs 指明了如果該包上傳則會被代理到npm公有倉庫,如果在下載某個不包含 @heyi 前綴的包時,會自動代理到npm公有倉庫查找資源並下載,並且默認會將拉取的資源緩存到我們前面指定的 storage 文件夾中。

listen

相信開發過后端的同學都不會陌生,服務啟動在什么端口,verdaccio默認監聽在4873端,我們可以通過指定 listen 參數修改配置

listen:
	0.0.0.0: 3000

設置完重啟 verdaccio,端口便監聽在了3000

到這里npm私有倉庫的配置啟動就完成了,接下來我們就可以開發包並發布了,但在這之前,推薦大家一個管理npm源的工具,nrm

優雅地方式設置npm源

安裝 nrm

使用 npm 安裝 nrm

npm i -g nrm

查看 nrm幫助

➜  ~ nrm -h
Usage: nrm [options] [command]
Options:
  -V, --version                           output the version number
  -h, --help                              output usage information
Commands:
  ls                                      List all the registries
  current                                 Show current registry name
  use <registry>                          Change registry to registry
  add <registry> <url> [home]             Add one custom registry
  set-auth [options] <registry> [value]   Set authorize information for a custom registry with a base64 encoded string or username and pasword
  set-email <registry> <value>            Set email for a custom registry
  set-hosted-repo <registry> <value>      Set hosted npm repository for a custom registry to publish packages
  del <registry>                          Delete one custom registry
  home <registry> [browser]               Open the homepage of registry with optional browser
  publish [options] [<tarball>|<folder>]  Publish package to current registry if current registry is a custom registry.
   if you're not using custom registry, this command will run npm publish directly
  test [registry]                         Show response time for specific or all registries
  help                                    Print this help

列出當前 nrm 存儲的npm源

nrm ls

結果如下

➜  ~ 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/

使用指定源

比如我們現在想使用npm官方源

nrm use npm

添加用戶自定義的源

添加命令很簡單,只需要指定源名和源地址即可

nrm add heyi 47.94.248.23:3000

再運行查看命令就能發現,列表中多了一條記錄,正是我們添加的源

登錄npm

注意,登錄前一定要保證此時的npm源是指向我們私有倉庫的,使用nrm即可完成切換

nrm use heyi

添加用戶

如果是第一次登錄,則需要注冊用戶,如果服務未做特殊權限的設置,直接添加用戶即可,命令如下

npm addUser

跟隨提示填寫用戶名、密碼、郵箱即可

發布包

npm publish

img

使用pm2啟動verdaccio

pm2 start verdaccio


免責聲明!

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



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