随着维护的项目越来越多,项目间的代码复用显得越来越重要。另外,团队内部的代码模块化,也需要有一个机制来管理。于是,我尝试使用私有npm搭建一套前端仓库,以便于在公司内部托管公共代码以及将代码模块化。
一、搭建环境
我这里是windows环境下,安装好node和npm。其中node的版本不能太低。
二、全局安装sinopia
npm install -g sinopia
为什么要用sinopia呢?因为Sinopia安装比较简单,有自己的迷你数据库,如果要下载的包不存在,将自动去配置的npm地址上自行下载,而且硬盘中只缓存你下载过的包,以节省空间。
三、修改配置文件
用记事本打开C:\Users\wsl\AppData\Roaming\sinopia\config.yaml
1.最后一行添加listen:0.0.0.0:4873
2.将默认的registry改为taobao的registry
#
# 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: F:/01Demo_Study/Npm私有仓库/lib_sinopia/storage
auth:
htpasswd:
file: F:/01Demo_Study/Npm私有仓库/lib_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/ #taobao的registry
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 #默认没有,只能在本机访问,添加后可以通过外网任意IP访问
四、运行sinopia
sinopia
运行结果如下,则为启动成功,就可以在浏览器上访问 http://localhost:4873/
warn --- config file - C:\Users\wsl\AppData\Roaming\sinopia\config.yaml
warn --- http address - http://0.0.0.0:4873/
五、创建用户
登陆到安装sinopia的服务器,执行
npm adduser --registry http:/localhost:4873
根据提示输入用户名和密码,邮箱可以随便填,创建成功后就可以用这个用户来登录私有仓库的管理后台了。
六、配置本地registry为私有仓库地址
安装nrm包
npm install -g nrm # 安装nrm
使用nrm可以管理仓库信息,添加私有仓库并且迷人使用这个仓库
npm set registry http://localhost:4873
等同于
nrm add name http://localhost:4873 # 添加本地的npm镜像地址
nrm use name # 使用本址的镜像地址 name为你要增加的地址
七、下载模块
添加完私有仓库之后,下载一个redis模块看看私有仓库是否生效
npm install -g redis
网友评论