Nexus 私服发布 npm 包流程记录

318 阅读1分钟

前言

Nexus Repository Manager 是一个仓库管理器,极大地简化了内部仓库的维护和外部仓库的访问,可用于搭建公司 maven、npm 内网仓库。公司内部 Nexus 私服网址如下:registry.abc.cn/#browse/wel…

管理员账号密码:admin/xxxabcxxx

步骤

1. 创建个人私服账号

image

2. 创建 blob store

创建一个用于 npm 包存储的 blob store,使用 file 类型,公司无 S3 类型的云存储空间,不选。

image

创建配置如下:

image

image

3. 创建 repository

image

image

创建成功后得到可发包的仓库地址:registry.abc.cn/repository/…

4. 设置 nexus 权限

image

5. 本地发包配置

shell 终端登录第一步创建的私服个人账号

npm adduser -registry http://registry.abc.cn/repository/stxz-npm-hosted/

image

6. 创建 npm 包并发布至私服

新建一个文件夹 hello-world,使用 npm init -y 进行初始化,添加 index.js 文件,内容如下:

module.exports = "Hello World!"

在 package.json中添加 publishConfig 设置,指定仓库地址。

"publishConfig": {
    "registry": "http://registry.abc.cn/repository/stxz-npm-hosted/"
}

使用 npm publish 发布。

image

image

7. 通过 npm 下载私服依赖包到本地使用

创建空项目 hello-world-test,使用 npm init -y 进行初始化,创建 index.js,并通过私服安装 hello-world 依赖。

npm install --save-dev hello-world --registry=http://registry.abc.cn/repository/stxz-npm-hosted/ 

image

index.js 内容如下

const helloWorld = require('hello-world')
console.error(helloWorld)

使用 node 执行 index.js

image

总结

后续有新的 npm 包创建或发布,只需要按步骤 1、5、6、7 操作即可,2、3、4 为一次性步骤, 使用 pnpm 发包命令与 npm 一致。