为什么80%的码农都做不了架构师?>>>
公司最近前端开发技术切到vue上,开发环境基于node.js,公用包不好管理,直接放在npm仓库上不合适,就打算在阿里云上搭建一套私有库。
搭建npm私有库方式有好几种,cnpm和sinopia,前者需要数据库支持配置复杂点,后者不需要数据库配置也简单,直接存储在文件夹中,所以选用sinopia。
安装环境及软件:
a) 、centOs6.5系统(版本比较老,现有的就没重新安装);
b)、node.js(sinopia需要基于nodejs环境)
c)、sinopia
d)、pm2(进程管理)
e)、nginx
f)、nrm(npm客户端管理)
1、首先需要安装node.js环境,sinopia是基于node.js环境运行的;
下载路径:https://nodejs.org/en/download/
如无特殊要求,选择打好的包,
[root@bjnja~]# wget https://nodejs.org/dist/v6.11.5/node-v6.11.5-linux-x64.tar.xz
解压包并重命名
[root@bjnja~]# tar xvf node-v6.11.5-linux-x64.tar.xz
[root@bjnja~]# mv node-v6.11.5-linux-x64 node
配置node_home环境变量;在/etc/profile
export NODE_HOME=/usr/local/nodejs/node
export PATH=$NODE_HOME/bin:$PATH
重新加载配置
[root@bjnja~]# source /etc/profile
现在node.js环境安装好了,试试
[root@bjnja~]# node -v
v6.11.4
2、安装sinopia,通过npm
[root@bjnja~]# npm install -g sinopia
下载完,启动sinopia
[root@bjnja~]# sinopia
warn --- config file - /root/.config/sinopia/config.yaml
warn --- http address - http://localhost:4873/
web管理界面端口是4873;现在只有本机可以访问,其他ip访问不了;
现在修改配置,找到/root/.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: /usr/local/sinopia/storage # 这是资源存储目录
auth:
htpasswd:
file: /usr/local/sinopia/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
'*':
# 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"
# 访问权限,三种:"$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}
# 把之前localhost:4873改0.0.0.0:4873,开发访问
listen: 0.0.0.0:4873
# 绑定域名,不然只通过nginx会报错未设置www啥的
url_prefix: http://npm.bjnja.com
3、通过pm2管理sinopia;
sinopia之前启动很容易端口,使用pm2进程管理器;
[root@bjnja~]# npm install -g pm2
使用pm2 start开启sinopia
[root@bjnja~]# pm2 start sinopia
[PM2] Applying action restartProcessId on app [sinopia](ids: 0)
[PM2] [sinopia](0) ✓
[PM2] Process successfully started
┌─────────┬──────┬────────┬───┬─────┬──────────┐
│ Name │ mode │ status │ ↺ │ cpu │ memory │
├─────────┼──────┼────────┼───┼─────┼──────────┤
│ sinopia │ fork │ online │ 0 │ 0% │ 4.9 MB │
└─────────┴──────┴────────┴───┴─────┴──────────┘
Use `pm2 show ` to get more details about an app
关闭pm2 stop sinopia
4、使用nginx配置代理
server {
listen 80;
server_name npm.bjnja.com;
location / {
proxy_pass http://127.0.0.1:4873;
}
}
5、客户使用使用nrm管理
[root@bjnja-client~]# npm install -g nrm
使用nrm add 添加我们私有库
[root@bjnja-client~]# nrm add nja http://npm.bjnja.com
使用nrm ls可以列出刚才我们加上的
[root@bjnja-client~]# 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/
* nja ---- http://npm.bjnja.com/
使用nrm use nja自由切换;然后就可以使用npm指令,这是npm源已经切换我们刚加的;
补充:现在sinopia作者不维护了,推荐使用verdaccio,是sinopia分支
https://github.com/verdaccio/verdaccio