简介

Verdaccio是个轻量的基于 Node.js 的 npm 私有仓库。
● 支持 npm 主流客户端(yarn/npm/pnpm),支持代理到其他 registry 地址及缓存加速。

docker-compose 方式运行(linux)

◇ 参考官方文档对 docker 方式的说明
◇ 指定一个存放路径。此处以此路径为例:/app/deploy/verdaccio。
◇ 在路径下创建三个子文件夹:config、plugins、storage。
◇ 由于 Verdaccio 在容器内使用的是 UID=10001 且 GID=65533 的非 root 用户,递归设置挂载目录的用户和用户组:

1
sudo chown -R 10001:65533 /app/deploy/verdaccio

◇ 参考官方示例配置文件在 config 文件夹下准备 config.yaml,示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# path to a directory with all packages
storage: /verdaccio/storage
# path to a directory with plugins to include, the plugins folder has the higher priority for loading plugins
# disable this folder to avoid warnings if is not used
# plugins: ./plugins

web:
enable: true
title: npm | example
logo: https://static-production.npmjs.com/b0f1a8318363185cc2ea6a40ac23eeb2.png
favicon: https://static-production.npmjs.com/b0f1a8318363185cc2ea6a40ac23eeb2.png
primary_color: '#3990f6'
showInfo: false
showSettings: false
showFooter: false
showUplinks: false
showDownloadTarball: false
showRaw: false

url_prefix: '/'
max_body_size: 300mb # max package's size 300M

auth:
htpasswd:
file: /verdaccio/storage/htpasswd # where to store login info
# Maximum amount of users allowed to register, defaults to "+inf".
# Set to -1 to disable registration.
max_users: -1

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

'**':
# you can specify usernames/groupnames (depending on your auth plugin)
# and three keywords: "$all", "$anonymous", "$authenticated"
access: $authenticated


publish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs
security:
web:
sign:
expiresIn: 30d # login expiration time, examples: 1h, 7d

log:
- { type: stdout, format: pretty, level: trace }
#- {type: file, path: verdaccio.log, level: info}

◇ 创建 docker-compose.yml 文件,内容示例如下。其中 VERDACCIO_PUBLIC_URL 不能为空,且需设置成域名网址而不能是相对路径(以 https://npm.my.cn 为例),否则 Verdaccio 将使用默认的 http://localhost:4873/ 作为所生成所有资源链接(如包的访问链接)的基础 URL。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name: verdaccio

services:
verdaccio:
image: verdaccio/verdaccio:6.0.2
container_name: 'verdaccio'
environment:
- VERDACCIO_PUBLIC_URL=https://npm.my.cn
- VERDACCIO_PORT=4873
ports:
- '4873:4873'
volumes:
- './storage:/verdaccio/storage'
- './config:/verdaccio/conf'
- './plugins:/verdaccio/plugins'

◇ 在 docker-compose.yml 文件同级目录下执行"docker compose up -d"命令启动。

私有仓库的使用

■ 以下私有仓库地址以 https://npm.my.cn 为例。

用户管理

■ 创建用户。在上述 config.yaml 配置中 max_users 不等于 -1 的情况下,执行:npm adduser --registry https://npm.my.cn。仓库连接成功会自动提示输入用户名密码等信息。
■ 手动登录。执行:npm login --registry https://npm.my.cn。
■ 自动登录。将登录信息保存到项目根目录的.npmrc 中可避免手动 login 的过程。示例如下。其中的<passwordToBase64>可通过执行"echo -n <password> | base64"得到。

1
2
//npm.my.cn/:username=<username>
//npm.my.cn/:_password=<passwordToBase64>

包的发布

■ 将包发布到私有仓库,在 publish 命令上增加参数:--registry https://npm.my.cn
■ registry 除了命令行参数方式之外,也支持添加到.npmrc 文件,以及 package.json 文件的 publishConfig 字段。三者优先级依次递减。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
//.npmrc 文件
registry=https://npm.my.cn


// package.json文件
{
... ...
"publishConfig": {
"registry": "https://npm.my.cn"
}
... ...
}

■ 包大小限制的调整:
(1)nginx 默认消息体大小限制 1M,通过 nginx 指令“client_max_body_size 300M”调整。
(2)verdiccio 默认包大小限制 10M,通过 config.yaml 中的“max_body_size: 300mb”调整。

包的安装

■ 所有包都从私有仓库下载。通过上述 --registry 命令行参数或 .npmrc 中的 registry 字段可实现。
■ 仅私有包从私有仓库下载。.npmrc 提供了为特定域或包名指定仓库地址的配置语法,但实践发现 pnpm 和 yarn 对这类语法的支持不完善或不统一,可通过将 package.json 的 dependencies 中包的版本号指定为带基础 URL 的完整下载地址实现,如:"demo-pkg": "https://npm.my.cn/demo-pkg/-/demo-pkg-0.0.11.tgz"。