使用verdaccio作为npm私库
安装
shell
# 安装
npm install -g verdaccio
# 启动
verdaccio
# 查看控制台输出,找到配置文件,并修改
修改配置
修改包存储目录
默认: (windows 默认在: C:\Users\username\AppData\Roaming\verdaccio\storage)
yaml
# path to a directory with all packages
storage: ./storage
改为:
yaml
# path to a directory with all packages
storage: E:/verdaccio-store
修改显示语言
默认:
yaml
i18n:
# list of the available translations https://github.com/verdaccio/verdaccio/blob/master/packages/plugins/ui-theme/src/i18n/ABOUT_TRANSLATIONS.md
web: en-US
改为中文
yaml
i18n:
# list of the available translations https://github.com/verdaccio/verdaccio/blob/master/packages/plugins/ui-theme/src/i18n/ABOUT_TRANSLATIONS.md
web: zh-CN
禁用注册用户
如果你的私有 npm 库部署在外网环境的话,任何人都可以通过 npm adduser 命令注册用户。显然,这是不允许出现的情况,所以这里我们需要设置 auth 的 max_users 为 -1,它代表的是禁用注册用户. 如果要开启用户注册,设置指定数字(大于 0)即可
默认
yaml
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
改为-1
yaml
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: -1
解决内网的其他用户无法访问 verdaccio 服务
config.yaml的listen必须配置成如下值
yaml
listen: 0.0.0.0:4873
避免外部仓库离线时无法发包
在发布包到 verdaccio 时, verdaccio 会检查依赖包有效性,这个过程中需要访问外部仓储。默认情况,如果外部仓储无法访问,则会发包失败,设置 allow_offline 为 true,则可以避免该问题
报出的错误类似这样:
http <-- 503, user: xx, req: 'PUT /helloworld', error: one of the uplinks is down, refuse to publish
原始
yaml
# https://verdaccio.org/docs/configuration#offline-publish
# publish:
# allow_offline: false
改为
yaml
# https://verdaccio.org/docs/configuration#offline-publish
publish:
allow_offline: true
解决将 verdaccio 设置为仓库后, 有时 pnpm install 卡住不动问题
一般是网络问题,给verdaccio设置多个代理源即可解决该问题
yaml
uplinks:
taobao:
url: https://registry.npm.taobao.org/
agent_options:
keepAlive: true
maxSockets: 40
maxFreeSockets: 10
npmjs:
url: https://registry.npmjs.org/
agent_options:
keepAlive: true
maxSockets: 40
maxFreeSockets: 10
# Learn how to protect your packages
# https://verdaccio.org/docs/protect-your-dependencies/
# https://verdaccio.org/docs/configuration#packages
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: taobao npmjs
用户操作
添加用户
npm adduser --registry http://localhost:4873/
删除用户
Verdaccio 默认使用的是 htpasswd 来实现鉴权。相应地,注册的用户信息会存储在 ~/.config/verdaccio/htpasswd 文件中。在该文件中一条记录对应一个用户,也就是如果这条记录被删除了,那么该用户就不能登陆了,即删除了该用户。
源操作
添加、切换源
shell
npm install -g yrm
shell
yrm add local-test http://localhost:4873/
local-test 是自定义的源别名
切换源
shell
yrm use local-test
发包
shell
npm publish
这个当你的包名为@your-name/your-package风格命名时,需要通过如下方式发包
shell
npm publish --access public
取消发布
shell
npm unpublish 包名|包名+版本 --force(可选)
为 指定组织下的包 配置特定仓库
情景描述:
公司内部开发的 npm 包,发布到了内网的 npm 私服中,只能通过公司的 npm 私服获取,同时公司的 npm 私服又无法连接外部网络。
此时就需要为公司内部包配置专门的 npm 私服,让这些依赖走私服下载,而其他普通的 npm 依赖包,依然走外部的 npm 仓库下载
在项目根目录创建.npmrc文件
yaml
@myOrg:registry=http://192.168.2.116