背景:
公司内部有很多个项目,其中有一些通用的组件或者插件,要在多个项目中共享,那要怎么办? copy ? copy ? copy ?那行copy吧,等你把所有的公用的东西都copy完成后,发现某一个地方修改了,那你是不是又要到每一个项目进行修改同步,整个人直接裂开!!
解决思路:
我们可以将公共的模块发布到npm
上去,这样要用到的项目直接用npm
命令安装即可。但是我又不想把公司的代码放到外网去,怎么办?那就私有化部署呗。
实现npm
包的私有化部署有好几种方案,我这里介绍一种比较简单又实用的,主角就是 verdaccio
,verdaccio
是在sinopia
的基础上成长的,sinopia
的作者已经对sinopia
停止更新,所以很多坑是没有解决的。因此强烈推荐使用verdaccio
。
npm包私有化部署步骤:
前置条件:已经安装好了node
环境,以及npm
命令
第一步:安装verdaccio
npm install -g verdaccio --unsafe-perm
加上–unsafe-perm
的原因是防止报grywarn
权限的错。
第二步: 启动
启动命令:verdaccio
启动后如下提示:
warn --- config file - /root/.config/verdaccio /config.yaml //verdaccio 的配置文件路径
warn --- http address - http://localhost:4873/ //verdaccio 服务启动端口默认为4873
这时候我们可以通过curl http://localhost:4873
访问,能正常访问表示verdaccio
服务已正常开启
备注1:如果提示command not found
,请移步这里:https://blog.csdn.net/weixin_41134409/article/details/90768753
备注2:可以用pm2
以守护进程的方式来启动verdaccio
,命令为:pm2 start verdaccio
,用pm2
方式启动verdaccio
后查看实时日志,命令
pm2 show verdaccio
如图:
如:查看错误日志命令
tail -f /root/.pm2/logs/verdaccio-error.log
第三部: 发布npm包
1、切换npm registry
(切换到我们直接内部的源)
建议客户端使用nrm
进行npm registry
地址管理和切换。了解更多nrm
命令请移步 https://blog.csdn.net/weixin_41134409/article/details/90897748
安装
npm install -g nrm
添加verdaccio
仓库地址
nrm add verdaccio http://192.168.xx.xx:4873
切换私有仓库
nrm use verdaccio
查看所有仓库地址(星标为当前仓库源)
nrm ls
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/
* verdaccio http://192.168.xx.xx:4873/
2、添加用户/登录
添加用户:
命令:npm adduser
,之后按提示输入密码和邮箱就添加成功。
登录:
命令:npm login
, 依次输入用户名、密码、邮箱
3、发布
命令:npm publish
//发布命令和发布到外网是一样的
通过以上步骤我们已经顺利发布到我们的私有化部署仓库去了,我们可以在浏览器访问http://私有化ip:4873
地址来查看我们发布的包。
最后介绍一下verdaccio
的配置文件config.yaml
:
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/verdaccio/tree/master/conf
# path to a directory with all packages
storage: ./storage //npm包存放的路径
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 //默认为1000,改为-1,禁止注册
# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/
//拉取公共包的地址源,默认为npm的官网,可以使用淘宝的npm镜像地址
packages: //配置权限管理
'@*/*':
# scoped packages
access: $all
publish: $authenticated
'*':
# 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 packages
# (anyone can register by default, remember?)
publish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs
# log settings
logs:
- {type: stdout, format: pretty, level: http}
#- {type: file, path: verdaccio.log, level: info}