搭建NPM私库-verdaccio
前言
通过网上相关网友推荐,使用verdaccio搭建为目前主流方案。
verdaccio
为什么选择verdaccio,因为sinopia到2015年10月后,已经早不维护了,似乎作者不见了踪影。而verdaccio是sinopia的一种新的web【react】端的实现形式,并且目前一直在维护中。
安装部署
环境安装
yum install epel-release vim gcc-c++ make -y
yum install nodejs -y
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
rm -f /etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
systemctl restart nginx
#首先安装nodejsyum install nodejs#安装完成后,查看版本 node -v
#输出v6.17.3#发现安装版本很低,觉得按照verdaccio后会有问题# 安装verdacionpm install --global verdaccio# 启动verdaccio#不出所料启动异常[root@izbp1dnj5vm2q9htc3s7bzz ~]# verdaccio/usr/lib/node_modules/verdaccio/build/lib/cli.js:92
(async () => {
^SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/lib/node_modules/verdaccio/bin/verdaccio:3:1)由于启动异常感觉应该是nodejs版本过低的原因,升级nodejs
升级nodejs
安装n,n是nodejs管理工具
npm install -g n #安装nodejs最新版本 n latest #若安装指定版本 n v13.1.0 #切换nodejs版本 n #选择已安装的版本 o node/13.1.0 installed : v13.1.0 (with npm 6.12.1) #查看当前版本 node -v node -v v6.17.1 #发现输出还是没更换 #修改配置文件 vim ~/.bash_profile #将下面两行代码插入到文件末尾: export N_PREFIX=/usr/local #node实际安装位置 export PATH=$N_PREFIX/bin:$PATH #执行source使修改生效 source ~/.bash_profile #查看当前版本 node -v 输出如下则切换成功 node -v v13.1.0
升级好nodejs后重新安装
#重新安装npm install --global verdaccio#启动 verdaccio*** WARNING: Verdaccio doesn't need superuser privileges. Don't run it under root! *** warn --- config file - /root/.config/verdaccio/config.yaml warn --- Verdaccio started warn --- Plugin successfully loaded: verdaccio-htpasswd warn --- Plugin successfully loaded: verdaccio-audit warn --- http address - http://localhost:4873/ - verdaccio/4.3.4
配置文件信息
# #号后面是注释
# 所有包的缓存目录
storage: ./storage
# 插件目录
plugins: ./plugins
#开启web 服务,能够通过web 访问
web:
# WebUI is enabled as default, if you want disable it, just uncomment this line
#enable: false
title: Verdaccio
#验证信息
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
# a list of other known repositories we can talk to
#公有仓库配置
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
'@*/*':
# scoped packages
access: $all
publish: $authenticated
#代理 表示没有的仓库会去这个npmjs 里面去找 ,
#npmjs 又指向 https://registry.npmjs.org/ ,就是上面的 uplinks 配置
proxy: npmjs
'**':
# 三种身份,所有人,匿名用户,认证(登陆)用户
# "$all", "$anonymous", "$authenticated"
#是否可访问所需要的权限
access: $all
#发布package 的权限
publish: $authenticated
# 如果package 不存在,就向代理的上游服务发起请求
proxy: npmjs
# To use `npm audit` uncomment the following section
middlewares:
audit:
enabled: true
# 监听的端口 ,重点, 不配置这个,只能本机能访问
listen: 0.0.0.0:4873
# log settings
logs:
- {type: stdout, format: pretty, level: http}
#- {type: file, path: verdaccio.log, level: info}访问web地址

web地址
使用pm2 守护verdaccio进程
安装pm2并使用pm2启动verdaccio,使用pm2托管的进程可以保证进程永远是活着的,尝试通过kill -9去杀verdaccio的进程发现杀了之后又自动启起来。推荐使用此种方式启动verdaccio.
安装pm2 npm install -g pm2 --unsafe-perm 3.2.2 使用pm2启动verdaccio pm2 start verdaccio 或 pm2 start `which verdaccio` 这样verdaccio就启动成功了。
访问私库
http://IP:4873
nignx配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# HTTPS server
#
# verdaccio registry
server {
listen 80;
server_name registry.npm.域名.work;
location / {
#proxy_redirect off;
proxy_pass http://127.0.0.1:7001;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭