linux环境npm包私有化部署

背景:
公司内部有很多个项目,其中有一些通用的组件或者插件,要在多个项目中共享,那要怎么办? copy ? copy ? copy ?那行copy吧,等你把所有的公用的东西都copy完成后,发现某一个地方修改了,那你是不是又要到每一个项目进行修改同步,整个人直接裂开!!

解决思路:
我们可以将公共的模块发布到npm上去,这样要用到的项目直接用npm命令安装即可。但是我又不想把公司的代码放到外网去,怎么办?那就私有化部署呗。

实现npm包的私有化部署有好几种方案,我这里介绍一种比较简单又实用的,主角就是 verdaccioverdaccio 是在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}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值