24

将npm本地包上传到nexus私服的实践

 2 years ago
source link: https://wiki.eryajf.net/pages/18ca89/#_6-%E5%8F%82%E8%80%83
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

将npm本地包上传到nexus私服的实践

# 1,前言

如何配置 npm 私服,这个问题很早之前就已经研究过,博客也发表过此文章: 使用 nexus3 配置 npm 私有仓库 (opens new window) ,本文来介绍当我们私服配置完毕之后,如何上传到nexus私服中。

这个逻辑跟 Python 私服有点类似:

其实用一句话来表述,就是 通过私服拉包的时候,走 group 的代理,从本地上传包的时候,要走 local 的仓库。

接下来就记录一下配置方法并用测试包来验证下。

创建私服的过程这里不详述了,已知现有 npm 私仓,物料信息如下:

  • group:http://192.168.31.68:8931/repository/npm/
  • local:http://192.168.31.68:8931/repository/npm-local/

本文将介绍一个通过本地上传的方式处理包依赖的流程。

# 2,准备

这个配置过程中,如果有一些地方没有配置正确,将会遇到一些错误,这里先介绍能够正常走通的流程,最后再汇总过程中可能会遇到的问题。

首先因为与私服交互需要一个账号,因此我们来创建一个单独的账号来与 npm 私服交互。

nexus 中账号的管理思路是,先创建角色,然后给角色绑定不同的管理权限,接着创建账号,将账号绑定给不同的角色。

先创建角色:

afa6929b47059291.png

这里创建一个 nx-npm 的角色,该角色拥有 npm 方面所有的权限,这可能是个比较大的权限,我没有测试过上传所需的最小权限,感兴趣的同学可以自行测试一下。

然后创建用户:

e553a28081843017.png

基本上都是常规内容,不做过多赘述。

注意给用户绑定 nx-npm 这个角色。

配置 nexus 中安全方面的一个规则,具体操作如下图,如果这步不操作,后边传包的时候会有 401 的错误。

8f676bc6ea80df5e.png

# 3,实践

以上准备工作完成之后,就可以进入传包的实践了。

首先创建一个空的测试包:

$ mkdir eryajf-test && cd eryajf-test
$ npm init -y && touch index.js

接着登陆认证到私服:

$ npm login --registry=http://192.168.31.68:8931/repository/npm-local/
Username: npm
Password:
Email: (this IS public) [email protected]
Logged in as npm on http://192.168.31.68:8931/repository/npm-local/.
1
2
3
4
5

然后执行上传:

$ npm publish --registry http://192.168.31.68:8931/repository/npm-local/
npm notice
npm notice 📦  [email protected]
npm notice === Tarball Contents ===
npm notice 0    index.js
npm notice 225B package.json
npm notice === Tarball Details ===
npm notice name:          eryajf-test
npm notice version:       1.0.0
npm notice package size:  278 B
npm notice unpacked size: 225 B
npm notice shasum:        179052169d980f939dc5051bac456ed6b8f3d217
npm notice integrity:     sha512-XcOCpbgz9deAK[...]/5mKTv7o5d8Xg==
npm notice total files:   2
npm notice
+ [email protected]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

从这里可以看到已经上传成功了,去浏览器当中查看:

f6942b1961b8e329.png

这样就成功将本地的包上传到了 nexus 私服当中。

# 4,测试

当我们把包成功上传之后,就可以直接通过如下命令测试包的下载:

$ npm i -S [email protected] --registry=http://192.168.31.68:8931/repository/npm/
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ [email protected]
added 1 package in 0.551s
1
2
3
4
5
6
7

可以看到包已经可以通过私服的 group 正常拉取了。

# 5,报错

  • 如果遇到如下报错:

    Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository > Manager"
    

原因是没有启用 npm Bearer Token Realm 参考上文进行启用即可。

  • 如果遇到 401 的报错,则大多是账号密码认证的问题。

# 6,参考


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK