Verdaccio是一个简单的、零配置要求的本地私有 npm 注册表。无需整个数据库即可开始!Verdaccio 开箱即用,带有 自己的小型数据库,并且能够代理其他注册表(例如 npmjs.org),并在此过程中缓存下载的模块。对于那些希望扩展其存储功能的人,Verdaccio 支持各种社区制作的插件,以连接到服务,例如 Amazon 的 s3、Google Cloud Storage或创建您自己的插件。
安装
使用 npm 安装:
npm install --global verdaccio
启动
运行启动命令:
verdaccio
在浏览器中输入 http://localhost:4873/ 出现如上页面就说明服务启动成功了。
修改监听的端口(可以使用默认)
找到终端中 config file 后面的那个配置文件(/Users/its_wild/.config/verdaccio/config.yaml) 在里面添加代码:listen: 0.0.0.0:3000
,如下图所示:
在终端中 ctrl + c 关闭 verdaccio 服务,再运行 verdaccio 启动服务,此时就可以能过 3000 端口访问服务了(http://192.168.66.17:3000/ 或 http://localhost:3000/ 或 http://127.0.0.1:3000/ ),效果和上面的一样。
修改语言(可以使用默认)
默认是 en-US 这里我们改成zh-CN,重启服务后就可以看到效果。
修改包的作用域
packages:
'@*/*':
# scoped packages
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs
packages:
'@its/*':
# scoped packages
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs
将 @/ 改为 @its/* 这样包名以 @its/ 开头的都只会在当前私有的仓库里发布和取消发布
使用 pm2 来管理进程
运行命令:
pm2 start verdaccio
这样就开启了 verdaccio 的服务,接下来我们可以使用这个搭建好的 npm 镜像服务器了,不会使用 pm2 的同学自行学习一下。
私有 npm 镜像服务器的使用
安装 nrm
npm install -g nrm
使用 nrm
nrm 安装好了后可以使用 nrm ls 查看可选的源
$ nrm ls
* npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
使用 nrm use <源> 可以切换源,例如切换成 cnpm 的源:
$ nrm use cnpm
Registry has been set to: http://r.cnpmjs.org/
使用 nrm add <源> < http://***.com/ > 可以添加源
添加私有 npm 镜像服务器的源
$ nrm add its http://192.168.66.17:3000/
add registry its success
$ nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
its ---------- http://192.168.66.17:3000/
使用私有 npm 镜像服务器的源
$ nrm use its
Registry has been set to: http://192.168.66.17:3000/
$ nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
* its ---------- http://192.168.66.17:3000
添加用户到私有 npm 镜像服务器
$ npm addUser
npm notice Log in on http://192.168.66.17:3000/
Username: its-wild
Password:
Email: (this IS public) 15288888888@163.com
Logged in as its-wild on http://192.168.66.17:3000/.
作用 npm addUser 添加用户成功后我们可以 使用 npm login 进行登录
$ npm login
npm notice Log in on http://192.168.66.17:3000/
Username: its-wild
Password:
Email: (this IS public) 15288888888@163.com
Logged in as its-wild on http://192.168.66.17:3000/.
发布包到私有 npm 镜像服务器
准备一个包
its
index.js
package.json
index.js 代码如下:
module.exports = () => {
console.log('恭喜您成功了~')
}
package.json 代码如下:
{
"name": "@its/wild",
"version": "1.0.0",
"description": "第一个私有包",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "its-wild",
"license": "ISC"
}
1.登录npm
$ npm login
npm notice Log in on http://192.168.66.17:3000/
Username: its-wild
Password:
Email: (this IS public) 15288888888@163.com
Logged in as its-wild on http://192.168.66.17:3000/.
2.发布
打开终端,进入its文件夹下,运行下面命令:
$ npm publish
npm notice
npm notice 📦 @its/wild@1.0.0
npm notice === Tarball Contents ===
npm notice 20B README.md
npm notice 63B index.js
npm notice 231B package.json
npm notice === Tarball Details ===
npm notice name: @its/wild
npm notice version: 1.0.0
npm notice filename: @its/wild-1.0.0.tgz
npm notice package size: 411 B
npm notice unpacked size: 314 B
npm notice shasum: 2b4a95cd08f43c09ba1fb35e0392dd60d4038f1d
npm notice integrity: sha512-7SuqgdT1GpBSP[...]F1SJpPeqo20/A==
npm notice total files: 3
npm notice
+ @its/wild@1.0.0
看到 + @its/wild@1.0.0 说明发布完成了。刷新浏览器可以看到发的包,效果如下:
使用私有 npm 镜像服务器的包
私有 npm 镜像服务器的包的使用和我们平时使用其他包是一样的。
1.新建文件夹 pack ,打开终端进入 pack 下,运行命令:
$ npm init -y
$ npm i @its/wild --save
2.新建 index.js ,内容如下:
const itsWild = require('@its/wild')
itsWild()
3.运行 node 命令:
$ node index
恭喜您成功了~
看到输出 恭喜您成功了~ 证明就可以了。
项目结构如下:
取消发布私有 npm 镜像服务器的包
运行如下命令:
$ npm unpublish @its/wild@1.0.0
- @its/wild@1.0.0
看到 - @its/wild@1.0.0 说明取消发布成功。
至此,使用 verdaccio 搭建 npm 镜像服务器整个过程记录完成。