使用 verdaccio
搭建本地仓库
1. Verdaccio 简介
Verdaccio,是一个轻量级的 npm 私有仓库的开源解决方案。npm是一个基于http的协议,用来存放软件包并且维护版本和依赖,利用 http 提供的 url路径 来对软件包进行增删改查。所以 Verdaccio 这款软件的核心就是实现 npm协议。
Verdaccio 有一个内置的数据库来存放所有的 npm 包,除此之外它还有一套默认的鉴权机制:htpasswd。htpasswd鉴权是通过htpasswd文件来存放所有的 npm用户,鉴权、添加/删除的时候通过对文件的读写来实现。
Verdaccio 提供了上行链路功能, 安装 Verdaccio 仓库中不存在的包时,可以配置向上游仓库下载。
Verdaccio 还提供了强大的身份认证系统。
2.使用教程
安装
npm install -g verdaccio
启动
verdaccio
3.常见问题
502 Bad Gateway
在执行 npm adduser --registry http://localhost:4873/
和 npm publish
时,出现如下报错:
C:\Users\Admin>npm adduser
npm notice Log in on http://localhost:4873/
npm ERR! code E502
npm ERR! 502 Bad Gateway - POST http://localhost:4873/-/v1/login
npm ERR! A complete log of this run can be found in: C:\Users\Admin\AppData\Local\npm-cache\_logs\2024-12-16T03_51_51_284Z-debug-0.log
通常是本地npm设置了代理导致的。
检查本地npm配置:
npm config list
出现如下配置
如果在npm list 中出现下面的代理配置删除
https-proxy = "http://127.0.0.1:7890"
proxy = "http://127.0.0.1:7890"
使用如下命令删除配置
npm config delete proxy
npm config delete https-proxy
再次重启 verdaccio 即可正常使用。
npm publish 发布不成功
执行npm publish 命令后,提示如下错误:
npm ERR! code ECONNRESET
npm ERR! syscall read
npm ERR! errno ECONNRESET
npm ERR! network request to http://localhost:4873/element-plus-kit failed, reason: read ECONNRESET
npm ERR! network This is a problem related to network connectivity.
npm ERR! network request to http://localhost:4873/element-plus-kit failed, reason: read ECONNRESET
npm ERR! network request to http://localhost:4873/element-plus-kit failed, reason: read ECONNRESET
npm ERR! network This is a problem related to network connectivity.
npm ERR! network request to http://localhost:4873/element-plus-kit failed, reason: read ECONNRESET
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network request to http://localhost:4873/element-plus-kit failed, reason: read ECONNRESET
npm ERR! network This is a problem related to network connectivity.
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
出现以上错误原因是你的网络无法连接到 npm 官网仓库 https://registry.npmjs.org/ 导致的,
解决方案
- 科学上网
- 找到
verdaccio
配置文件**/**/verdaccio/config.yaml
,修改uplinks
下的npmjs
的url地址,可将url配置成taobao镜像源
uplinks:
npmjs:
url: https://registry.npmmirror.com/ // [!code focus]
再次重启 verdaccio 即可正常使用。