因为工作中多个项目同步开发,vue公共前端模块需要共享,但是又不能传到公共npm上,所以需要搭建私有npm服务。经过调研,采用了sinopia的解决方案,具体搭建步骤如下:
1、Centos下安装npdejs:
curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -
yum -y install nodejs
2、通过npm安装sinopia
npm install -g sinopia
3、因为sinopia不再root账号下运行,故进入其他账号(我用的是app),然后启动sinopia
sinopia
4、打开/home/app/.config/sinopia/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/sinopia/tree/master/conf # # path to a directory with all packages storage: /home/app/.local/share/sinopia/storage 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 # log settings logs: - {type: stdout, format: pretty, level: http} #- {type: file, path: sinopia.log, level: info} # you can specify listen address (or simply a port) listen: 0.0.0.0:4873 # a list of other known repositories we can talk to uplinks: npmjs: url: https://registry.npmjs.org/ 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: sinopia.log, level: info}
# you can specify listen address (or simply a port)
listen: 0.0.0.0:4873
红字部分为新增加,如果没有这部分监听,外部客户端不能访问该sinopia服务
5、对sinopia服务使用pm2守护,安装pm2守护服务
npm install -g pm2
6、杀死原来单独启动的sinopia服务后,通过pm2来进行启动
pm2 start sinopia
7、如果centos开启了防火墙,请先将防火墙关闭,或者将4873端口放开,此时客户端就可以通过地址http://serviceIP:4873地址来进行访问了