使用 Verdaccio 搭建私有 NPM 仓库

稍有规模的前端团队可能会希望使用私有仓库托管 npm 包,使用 Verdaccio 可以快速搭建一个轻量级的私有 NPM 仓库。

基本需求

  • 可以以私有化的形式托管 npm 包
  • 可以代理 npm、yarn、taobao 等公开镜像源(在私有仓库里找不到的库应该自动溯源到公开镜像源)
  • 发布包的时候可以在钉钉群推送一个发包通知

技术选型

06

快速部署

部署目录

config
  - config.yaml # 注意这里后缀是 yaml,不是 yml
  - htpasswd
plugins
storage
docker-compose.yml

docker-compose.yml

version: "3.1"

services:
  verdaccio:
    image: verdaccio/verdaccio
    container_name: "verdaccio"
    networks:
      - node-network
    environment:
      - VERDACCIO_PORT=4873
    ports:
      - "4873:4873"
    volumes:
      - "./storage:/verdaccio/storage"
      - "./config:/verdaccio/conf"
      - "./plugins:/verdaccio/plugins"
networks:
  node-network:
    driver: bridge

config/config.yaml

storage: /verdaccio/storage/
plugins: /verdaccio/storage/
web:
  enable: true
  title: 私有 NPM 仓库
  gravatar: true
  login: true
  pkgManagers:
    - npm
    - yarn
    - pnpm
  html_cache: true
  showFooter: false
auth:
  htpasswd:
    file: ./htpasswd
    algorithm: md5 # 这里选择 md5 作为加密算法
    max_users: -1 # 不允许自由注册
i18n:
  web: zh-CN

notify: # 配置 Webhook 推送到钉钉,记得修改 access_token 和 atMobiles
  method: POST
  headers: [{ "Content-Type": "application/json" }]
  endpoint: https://oapi.dingtalk.com/robot/send?access_token=xxxx
  content: '{"msgtype":"text", "at": {"atMobiles": ["13000000000"] }, "text":{"content":"NPM 发布新包:\n > 包名称:{{name}} \n > 版本号:{{#each versions}}{{version}}{{/each}} \n > 发布者:{{publisher.name}} "}}'

uplinks:
  npmjs:
    url: https://registry.npmjs.org/
  yarn:
    url: https://registry.yarnpkg.com/
    timeout: 10s
  taobao:
    url: https://npmmirror.com/
    timeout: 10s

packages:
  "@my-company/*":
    access: $all # "$all", "$anonymous", "$authenticated"
    publish: $authenticated
    unpublish: $authenticated
  "@*/*":
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs yarn taobao
  "**":
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs yarn taobao
middlewares:
  audit:
    enabled: true
listen: 0.0.0.0:4873
log: { type: stdout, format: pretty, level: http }

查看配置文件所有参数

config/htpasswd

内置的简易账号密码管理,这里暂时添加两个用户 user1/user1user2/user2

user1:$apr1$ZIzenKzu$lTLaynrdOvN7js5kEUeva1
user2:$apr1$i8xp.xlU$uM4Sc0di1TytrjAj5xEhU1

在线 htpasswd 生成

启动服务

docker-compose up -d

🎉 访问 http://localhost:4873

01

立即使用

登录仓库

npm adduser --registry http://localhost:4873

账号密码使用我们在 htpasswd 里添加的用户

发布包

npm publish --registry=http://localhost:4873

02

安装包

npm install @my-company/first-package --registry=http://localhost:4873

# 得益于 Verdaccio 的 proxy 特性,即使私有仓库里不存在的包也可以被下载到
# 取决你配置的代理策略,第一次会自动溯源到 npm、yarn、taobao 等上游镜像源
npm install lodash --registry http://localhost:4873

你也可以全局修改 npm、yarn 或 pnpm 的源配置,npm 方式如下

npm set registry http://localhost:4873
npm adduser --registry http://localhost:4873
npm profile set password --registry http://localhost:4873

其它工具请参考其对应使用文档,大同小异

钉钉推送

钉钉群助手 - 添加机器人 - 自定义

05

03

常见问题

无法发布

错误信息: 'xxx' is not in this registry

解决办法: 给 storage 文件夹调整读写权限

chmod -R 777 storage/

无法启动

错误信息: verdaccio | fatal--- cannot open config file /verdaccio/conf/config.yaml: Error: CONFIG: it does not look like a valid config file

解决办法: 确保 config.yaml 文件后缀名(不能是 yml)和语法均正确

钉钉无法推送

  • 查看服务所在机器是否可以访问公网
  • 查看钉钉机器人的安全配置是否匹配,配置了“关键词”的需要确保请求体中带有对应关键词