0%

将npm包发布在私有仓库nexus中

有时候需要将自己写的一些前端项目打包发布到公司内部的 nexus 仓库中
私服搭建地址:https://www.jianshu.com/p/1cfbc1518fce

在 nexus 中新建一个 repository

打开 nexus,登录以后,按照下图操作

选择 npm (hosted)

填写 repository 相关信息

  1. 这里的 Blob Store 最好选择为 npm 专属的
  2. 如果没有需要在 Blob Stores 新建
  3. 在 Hosted 选择: Allow redeploy >
  4. 点击 Create repository 创建

配置 npm

查看仓库地址

在 Repositories 列表中选择刚刚建的 npm-hosted ,点击 copy

在弹出的弹框中可以看到仓库地址

配置仓库地址

在 npm 中配置仓库地址,执行命令:

1
npm config set registry 仓库地址

验证配置是否正确

执行命令

1
npm config list

添加 nexus 权限

Realms 菜单中,将 npm Bearer Token Realm 添加到 Active

上传 nexus

新建一个 demo-test 项目

  1. 新建一个目录 demo-test,并切换到其中
1
mkdir demo-test && cd demo-test
  1. 初始化一个项目
1
npm init

然后一路回车,最后键入 y 即可

添加用户

1
npm adduser -registry 仓库地址
1
2
3
4
5
npm adduser -registry http://xxx.xx.x.x:xxxx/repository/npm-hosted/
Username: admin
Password:
Email: (this IS public) demo-test@devops.com
Logged in as admin on http://xxx.xx.x.x:xxxx/repository/npm-hosted/.

上传包

上传的包一定要确保根目录下有 package.json ,否则会报错。

第一种方式

执行命令:

1
npm publish -registry 仓库地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
demo-test npm publish -registry http://xx.xx.xx.xx:xxx/repository/npm-hosted/
npm notice
npm notice 📦 demo-test@1.0.0
npm notice === Tarball Contents ===
npm notice 348B package.json
npm notice === Tarball Details ===
npm notice name: demo-test
npm notice version: 1.0.0
npm notice package size: 320 B
npm notice unpacked size: 348 B
npm notice shasum: c897041e0aa1cbf68734be9e4058e39490de8eb3
npm notice integrity: sha512-Seb61XX1ronMM[...]ioQBdXs2fEW+Q==
npm notice total files: 1
npm notice
+ demo-test@1.0.0
➜ demo-test

此时即上传成功

第二种方式

在 package.json 中添加

1
2
3
"publishConfig": {
"registry": "仓库地址"
},

然后执行

1
npm publish

验证是否上传成功