Verdaccio 是一个简单的、零配置本地私有 npm 软件包代理注册表。Verdaccio 开箱即用,拥有自己的小型数据库,能够代理其它注册表(例如 npmjs.org),缓存下载的模块。此外 Verdaccio 还易于扩展存储功能,它支持各种社区制作的插件,以连接到亚马逊的 s3、谷歌云存储等服务或创建自己的插件。
为什么会搞这个?
是想把我们公司的npm私有仓从nexus迁移出来.
我们目前和后端共用nexus,因为nexus也能管理maven这些!
npm私有仓库独立出来,后续好推向整个公司!!
我们这里采用docker自己部署(用的是v4.6.x),方便后续滚动更新.
配置文件及模块缓存目录是从外部映射进去,维护比较弹性
外网访问是通过nginx代理,内网集群是通过安全组策略授权.
总体来说对前端人员比较友好,很多信息很直观.
包括依赖,发布仓库,代码反馈,node版本等等
verdaccio配置主要集中在一个配置文件, config.yaml
web:
# WebUI is enabled as default, if you want disable it, just uncomment this line
enable: true
title: #网站首页进入的正文标题
logo: #这里可以给定一个远程连接的图片,注释掉就采用默认的
# comment out to disable gravatar support
gravatar: true
# by default packages are ordercer ascendant (asc|desc)
# sort_packages: asc # 包的排序
# darkMode: true # 黑暗模式
# scope: "@scope"
# translate your registry, api i18n not available yet
i18n:
# list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations
web: zh-CN # 默认是en-US,我们改为默认中文,这个东东支持多语言
包安装索引资源顺序
我们通过这个仓库安装资源有顺序的,是依次之上往下检索!!
值得一提的是: proxy
没必要同时代理多个公网的源,会非常慢!!!因为会轮询去找资源!!
packages:
'@h3/*': # 若是@h3开头的包优先检索
access: $all #谁可以访问
publish: linqh jira # 谁可以发布(可以授权个人用户或者组,比如我们这里是我还有我们jira的所有用户)
unpublish: linqh # 谁可以撤包!!就会把包从私有仓下架!!!而非npm那样只打deprecated标记位
proxy: nexus-yunshu # 这里就是关联上游链了,uplinks,支持多个上游链
'**': # 最终索引的地方,是不是很像路由的概念,
# 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 packagesnonymous
# (anyone can register by default, remember?)
publish: $authenticated
unpublish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: taobao nexus-yunshu # 公网包我们优先从淘宝镜像源上拉取,之后查询我们nexus之前的私有包
效果图字段展示
主要是读取package.json的标准字段来实现的,
代码的部分数据做了脱敏!!!我自己的名字就无所谓了~~
json里面加了些注释,可以瞅瞅
{
"name": "test-ci",
"version": "0.5.0",
"description":"这只是一个测试发包的例子,包括用来测试ci/cd的,请勿下载使用!!!", # 包描述
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"jk2dt": "jk2dt"
},
"keywords": [ # 给别人检索的关键字
"test",
"test-verdaccio",
"ci",
"ci/cd",
"demo",
"example"
],
"author": "linqunhe", # 作者
"contributors": [ # 贡献者
{
"name": "xxx",
"email": "xxxm"
},
{
"name": "xx2",
"email": "xx2@xxx.com"
},
{
"name": "xx3",
"email": "xx@xx.com"
}
],
"dependencies": { # 核心依赖
"core-js": "^3.6.5",
"vue": "^2.6.11"
},
"devDependencies": { # 开发依赖
"@h3/jenkins-2-dingtalk": "^1.7.10",
"@vue/cli-plugin-babel": "~4.4.0",
"@vue/cli-plugin-eslint": "~4.4.0",
"@vue/cli-service": "~4.4.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
],
"peerDependencies": { # 引用的关联依赖,不会强制安装,缺失会警告
"@h3/antd-vue": ">=1.4.10",
"lodash": ">=4.17.15",
"vue": ">=2.6.11",
"vue-template-compiler": ">=2.6.11"
},
"bugs": { # 对应code repo的issue
"url": "httxxxxk/issues",
"email":"cxxx"
},
"engines": { # 可以告知该报依赖什么node版本乃至什么版本的npm
"node": ">= 12.0.0",
"npm" : "^6.0.0"
},
"repository": { # code repo
"type": "git",
"url": "httpxxxx-hook.git"
},
"publishConfig": { # 指定发布域,就是指向私有仓
"registry": "http://xxx"
},
"homepage": "httxxxxdy/test-ci-hook#readme",
"license": "MIT"
}
项目资源安装服务端错误500
请依次排除以下三点
- 代理的上游链互相引用,请保持单一!
- 本地缓存异常
- 安装区域先清空缓存
npm cache clear -f
再安装
- 终极大法(慎用)
清空verdaccio的模块缓存目录,这样所有依赖会重新梳理(已经发布的私有包注意备份)
安装
使用centos,也可以使用docker镜像
安装nodejs
yum install -y nodejs
安装verdaccio
npm install -g verdaccio --unsafe-perm
配置
a.修改配置文件 config.yaml,在其最后添加监听端口(使其可在外网访问)
listen: 0.0.0.0:4873
b.对外开放4873端口
firewall-cmd --state # 先查看防火墙状态,
service firewalld start # 开启防火墙:
firewall-cmd --zone=public --add-port=4837/tcp --permanent
firewall-cmd --zone=public --add-port=4837/udp --permanent #开放4873端口
firewall-cmd --reload #重新载入
firewall-cmd --zone=public --query-port=4873/tcp #查看是否添加成功
启动verdaccio
verdaccio
浏览器打开地址
使用pm2启动verdicco(pm2托管的进程可以保证进程永远是活着的,尝试通过kill -9去杀verdaccio的进程发现杀了之后又自动启起来)
全局安装pm2
npm install -g pm2 --unsafe-perm
使用pm2启动verdicco
pm2 start `which verdaccio`
添加用户
npm adduser --registry http://192.168.XX.XX:4873 //后面是我们的私服地址
类似如下:
Username: lk
Password:
Email: (this IS public) lk@qq.com
Logged in as rong on http://192.168.XX.XX:4873/.
然后在verdaccio启动页面尝试登录,默认登录后有发布包的权限。