为什么要搭建npm私有库?
- 为了方便下载时,公共包走npmjs,私有包走内部服务器。
- npm包下载的速度较慢,搭建npm私有库之后,会先操作私有库中是否有缓存,有缓存直接走缓存,而不用重新再去请求一遍网络。
哪种方式适合你呢?
npm私有库的搭建有很多种,具体哪种方式适合,我选择的方案是比较简单的“使用verdaccio搭建npm私有库”。
先试着在本地搭建一个吧
-
准备工作
我们需要使用npm命令去安装verdaccio,所以我们必须要有node环境,node环境又依赖于python。因此,在搭建npm私有库的准备工作就是去搭建node环境。
-
检测是否有node环境
chenwentaodeiMac:ceair_wallet chenwentao$ node -v bash: node: command not found -
安装node
-
检验是否安装成功
chenwentaodeiMac:ceair_wallet chenwentao$ node -v v10.15.1
-
-
安装启动verdaccio
-
安装verdaccio
安装速度缓慢的话,可以使用淘宝镜像,install时遇到permission denied,记得前面加sudo
chenwentaodeiMac:ceair_wallet chenwentao$ sudo cnpm install -g verdaccio -
启动verdaccio
启动成功后,打开http://localhost:4873/,看到界面就表示成功了
chenwentaodeiMac:ceair_wallet chenwentao$ verdaccio warn --- config file - /Users/chenwentao/.config/verdaccio/config.yaml //配置文件 warn --- Plugin successfully loaded: htpasswd //保存用户账户、密码等信息 warn --- Plugin successfully loaded: audit warn --- http address - http://localhost:4873/ - verdaccio/3.11.4 //地址
-
-
配置文件
默认的配置文件允许所有的用户拥有任何的权限。
# # 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/verdaccio/verdaccio/tree/master/conf # # path to a directory with all packages 存储npm包的路径 storage: ./storage # path to a directory with plugins to include plugins: ./plugins web: # WebUI is enabled as default, if you want disable it, just uncomment this line # web页面的配置 即上面的http://localhost:4873/ 默认为可访问。title就是标题,可以修改 #enable: false title: Verdaccio auth: # 保存用户账户、密码等信息文件,可以将max_users设置为-1禁止用户添加,从而通过修改htpasswd来添加用户 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 # a list of other known repositories we can talk to # 访问公共库的路径,可以修改成淘宝镜像 https://registry.npm.taobao.org uplinks: npmjs: url: https://registry.npmjs.org/ packages: '@*/*': # scoped packages access: $all publish: $authenticated proxy: npmjs '**': # 配置权限 # 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 # You can specify HTTP/1.1 server keep alive timeout in seconds for incomming connections. # A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout. # WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enought. server: keepAliveTimeout: 60 # To use `npm audit` uncomment the following section middlewares: audit: enabled: true # log settings logs: - {type: stdout, format: pretty, level: http} #- {type: file, path: verdaccio.log, level: info} # 配置之后相同wifi下其他电脑也可以访问了 访问地址为你的ip加上端口4873 listen: 0.0.0.0:4873 -
客户端配置
本地的私有仓库已经搭建好了,接下来我们需要通过客户端配置registry来使用我们的私有仓库。在浏览器中打开http://10.68.18.154:4873/时,会有提示(10.68.18.154是本机的IP地址)
Login:
npm adduser --registry http://10.68.18.154:4873Publish:
npm publish --registry http://10.68.18.154:4873
在linux服务器上尝试一下
刚才,我们在本地构建了一个npm私有库,现在我们到Linux服务器上尝试一下吧。首先,检测一下有没有安装node和python,如果没有安装就进行安装,那么我们接下来来安装一下。
-
安装python
在Linux上安装python,需要用命令行去操作。
下载
解压
-
安装node
下载
解压
接下来和本地一样去创建npm私有库,创建完之后让我们永久的运行verdaccio吧。
-
永久运行verdaccio
sudo npm install -g forever forever start `which verdaccio`