nexus支持很多的format,前一篇讲了作为maven的私服,现在介绍下怎么来作为npm的私服,其实和前面创建maven仓库的过程基本一样,可以说是驾轻就熟了,哈哈。这样的话一个nexus就解决了我们公司的前后端的私服问题,好爽啊!

一、创建Blob Stores

和maven一样,这里新建一个blob store专门作为npm的存储。

点击"Repository"–>“Blob Store”–>“Create blob store”

1)Type
选择"File"。

2)Name
就叫npm-blob吧。

3)Enable Soft Quota
限制目录的大小。我这边就不限制了。如果要限制的话,就勾选上,并填上限制的条件和限制的值就OK了。

4)Path
在填入Name之后,path会自动生成。

nexus配置maven proxy nexus配置npm仓库_linux

最后点击"Create blob store"就完成了。

nexus配置maven proxy nexus配置npm仓库_linux_02

二、创建一个hosted类型的仓库

用户可以把一些自己的构件手动上传至宿主仓库(Hosted Repository)中。

点击"Repository"–>“Repositories”–>“Create repository”,选择npm(hosted)。

nexus配置maven proxy nexus配置npm仓库_nexus配置maven proxy_03

1)Name
就叫npm-hosted-my吧。

2)Online
勾选,可以设置这个仓库是在线还是离线。

3)Storang
Blob store:选择此仓库使用的Blob存储,这里选择之前创建的npm-blob。
Strict Content Type Validation:验证上传内容格式,这里就用默认的勾选。

4)Hosted
Deployment Policy:部署策略,有三个选项,分别是:
Allow Redeploy:允许重新部署
Disable Redeploy:禁止重新部署
Read-Only:只读

这里使用默认的"Disable Redeploy",如果是开发环境,可以选择"Allow Redeploy"。

5)Cleanup
Cleanup Policies:清除策略,这个是新增的功能,这里先不进行设置。

配置完成后如下图

nexus配置maven proxy nexus配置npm仓库_上传_04

三、创建一个proxy类型的仓库

代理仓库(Proxy Repository)是远程仓库的代理,当用户向这个代理仓库请求一个依赖包时,这个代理仓库会先在本地查找,如果存在,会直接提供给用户进行下载;如果在代理仓库本地查找不到,就会从配置的远程中央仓库中进行下载,下载到私服上之后再提供给用户下载。所以一般我们把私服架设在内网之中,这样可以节省外网带宽,并且大大提高了用户下载依赖的速度。

点击"Repository"–>“Repositories”–>“Create repository”,选择npm(proxy)。

1)Name
因为我要代理淘宝的npm,所以就叫"npm-proxy-taobao"。

2)Online
勾选,设置成在线。

3)Proxy
Remote storage:设置远程中央仓库的地址,我这里设置成淘宝的npm仓库地址—https://registry.npm.taobao.org

其他的用默认值即可。

4)Storage
Blob store:选择npm-blob
Strict Content Type Validation:验证上传内容格式,这里就用默认的勾选。

5)Routing,Negative Cache,Cleanup,HTTP
都使用默认配置。

nexus配置maven proxy nexus配置npm仓库_linux_05

四、创建一个group类型的仓库

仓库组(Repository Group)的目的是将多个仓库(代理仓库和宿主仓库)聚合,对用户暴露统一的地址。当用户需要获取某一个依赖包时,请求的是仓库组的地址,系统将会根据仓库组配置的仓库顺序依次查找。

点击"Repository"–>“Repositories”–>“Create repository”,选择npm(gruop)。

1)Name
npm-group-my

2)Online
勾选,设置成在线

3)Storage
Blob store:选择npm-blob
Strict Content Type Validation:使用默认的勾选

4)Group
将左侧的Available中的仓库列表添加到右侧的Members中。

nexus配置maven proxy nexus配置npm仓库_nexus_06

五、验证测试

那用什么项目测试呢?这里我用vue脚手架生成了一个测试项目,放到了github上,地址为https://github.com/wangchaoforever/nexus_npmtest.git,并且再准备一台服务器对上面的前端代码进行打包测试。

1、安装npm

npm是Node.js一起发布的,只是安装了Node.js,npm也就安装好了。可以从node的下载页中下载对应操作系统的安装包进行安装即可。

下载地址为https://nodejs.org/en/download/

nexus配置maven proxy nexus配置npm仓库_json_07

选择长期支持版本的linux版本进行下载。

安装node很简单,步骤为:解压–>添加环境变–>使环境变量生效–>验证

[root@localhost src]# xz -d node-v10.16.3-linux-x64.tar.xz 
[root@localhost src]# tar -xf node-v10.16.3-linux-x64.tar -C /usr/local/
[root@localhost src]# vim /etc/profile
[root@localhost src]# tail -n 2 /etc/profile
export NODE_HOME=/usr/local/node-v10.16.3-linux-x64
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:$NODE_HOME/bin
[root@localhost src]# source /etc/profile
[root@localhost src]# node -v
v10.16.3
[root@localhost src]# npm -v
6.9.0

2、验证从私服下载

用下面的命令设置npm的仓库地址

[root@localhost ~]# npm config set registry http://192.168.0.125:9081/repository/npm-group-my/

这个时候再用户目录下会生成一个.npmrc文件,这个文件中的内容就是上面设置的npm仓库的地址。

我们把.npmrc中的内容补充完整,主要是要把私服的用户名密码配置上去(因为后面的上传时需要做用户名密码验证的)。

[root@localhost ~]# cat .npmrc 
registry=http://192.168.0.125:9081/repository/npm-group-my/
email=wang@qq.com
always-auth=true
_auth="YWRtaW46QWJjQDEyMzQ1Ng=="

"""
#_auth的值是nexus的username:password转成的base64的值,这样设置的好处是publish时就不用login了
"""

再将上面github上的代码克隆到服务器上。

nexus配置maven proxy nexus配置npm仓库_nexus配置maven proxy_08

在打包之前,先看一眼现在私服上的npm-group-my仓库是没有任何的构件的。

nexus配置maven proxy nexus配置npm仓库_linux_09

好了,万事具备,开始安装测试项目的依赖吧。

[root@localhost test3]# ls
nexus_npmtest
[root@localhost test3]# cd nexus_npmtest/
[root@localhost nexus_npmtest]# ls
babel.config.js  package.json  package-lock.json  public  README.md  src
[root@localhost nexus_npmtest]# npm install

依赖模块安装完成

nexus配置maven proxy nexus配置npm仓库_上传_10

然后再看一眼私服上的npm-group-my仓库。安装的依赖模块太多了,就截取一部分吧。

nexus配置maven proxy nexus配置npm仓库_nexus_11

跟预期的一样,私服上已经从远程中央仓库下载了依赖模块。

3、验证上传

1)修改package.json

1-1)把private的值修改为false

nexus配置maven proxy nexus配置npm仓库_nexus配置maven proxy_12

1-2)在json中加上publishConfig,这里需要注意的是,发布到的是hosted仓库,而不是group仓库。

nexus配置maven proxy nexus配置npm仓库_nexus配置maven proxy_13

如果不想在package.json中配置,也可以在publish的时候指定。

[root@localhost nexus_npmtest]# npm publish --registry=http://192.168.0.125:9081/repository/npm-hosted-my/

2)上传构件

在项目的根目录执行npm publish即可。

nexus配置maven proxy nexus配置npm仓库_nexus配置maven proxy_14

可以看到,已经把test@0.1.0上传了,这时候去私服看一下npm-hosted-my仓库。

nexus配置maven proxy nexus配置npm仓库_nexus_15

有了,完美上传了,哈哈。

参考文章:
https://help.sonatype.com/repomanager3/formats/npm-registry

https://blog.sydy1314.com/posts/56354/
https://www.jianshu.com/p/1674a6bc1c12
http://www.eryajf.net/1956.html