使用Verdaccio搭建NPM私有仓库

第一步:安装Node

安装node:

wget https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.xz

解压文件重命名:

xz -d node-v12.16.3-linux-x64.tar.xz 
tar -xvf node-v12.16.3-linux-x64.tar
mv node-v12.16.3-linux-x64 nodejs

设置快捷方式:

ln -s /root/nodejs/bin/node /usr/local/bin/node 
ln -s /root/nodejs/bin/npm /usr/local/bin/npm 

测试:

node -v
npm -v

第二步:安装verdaccio

npm install --global verdaccio

安装pm2并使用pm2启动verdaccio,使用pm2托管的进程可以保证进程永远是活着的

npm install --global pm2

设置快捷方式:

ln -s /root/nodejs/bin/verdaccio /usr/local/bin/verdaccio
ln -s /root/nodejs/bin/pm2 /usr/local/bin/pm2

使用pm2启动verdaccio:

pm2 start verdaccio

2020-06-19T03:57:19.png

配置:

2020-06-19T03:58:32.png

打开config.yaml文件

vim /root/.config/verdaccio/config.yaml

在配置文件最后加上

listen: 0.0.0.0:4873

就可以根据ip地址去访问了。

添加用户

一种是通过命令添加

npm adduser --registry http://xxx.xxx.xxx.xxx:4873  

另外一种通过htpasswd工具,选择Crypt方式创建密码,然后粘贴到htpasswd文件中。

添加完用户之后,建议关闭用户注册,配置 max_users: -1 即可关闭。

发布

切换到私有npm源

npm set registry http://xxx.xxx.xxx.xxx:4873
npm login
npm publish

2020-06-19T05:57:15.png

安装nrm

npm install --global nrm

设置快捷方式:

ln -s /root/nodejs/bin/nrm /usr/local/bin/nrm 

常用的镜像:

npm ---- https://registry.npmjs.org/
cnpm --- http://r.cnpmjs.org/
taobao - https://registry.npm.taobao.org/
nj ----- https://registry.nodejitsu.com/
rednpm - http://registry.mirror.cqupt.edu.cn/
npmMirror  https://skimdb.npmjs.com/registry/
edunpm - http://registry.enpmjs.org/

nrm常用命令:

查看可选源

nrm ls

查看当前使用源

nrm current

切换源,比如:切换为taobao源

nrm use taobao

添加源 registry为源名,url为源地址

nrm add <registry> <url>

删除源

nrm del <registry>

关于PM2的命令

# 列表
pm2 list
# 重启
pm2 restart app_name|app_id
# 停止
pm2 stop app_name|app_id
# 删除
pm2 delete app_name|app_id

添加新评论