nexus 的 npm 私库应用(Repository 创建)

994 阅读3分钟

nexus 的 npm 私库应用(Repository 创建)

1. 背景介绍

哈喽,大家好啊!在上一篇 nexus 搭建 npm 私库 中,花了九牛二虎之力,终于将 nexus 给安装好了。得试试好不好用,是时候折腾下了!

2. 登录

访问配置好的页面地址,出现以下页面,点下 Sign in

Untitled.png

根据登录弹窗的提示信息,找到登录密码。建议找个小本本记录下来,已备后用。

root@debian-gnu-linux-11:/opt/nexus# cat /opt/nexus/sonatype-work/nexus3/admin.password
8216b4ff-cf01-4003-a215-6c82b2f667c6

拿到密码登录,根据提示进行修改密码操作,然后一步步设置四下,就可以开始我们的 npm 私库之旅。

3. 创建 Blob Store

点击小齿轮进入设置页面,选中 Blob Stores ,再点击 Create Blob Store 按钮。

Untitled 1.png

这里有段说明什么是 blob store ,大家可以看看,有助于理解哈。

The binary assets you download via proxy repositories, or publish to hosted repositories, are stored in the blob store attached to those repositories. In traditional, single node NXRM deployments, blob stores are typically associated with a local filesystem directory, usually within the sonatype-work directory. For more information, check the documentation.

填入必要的信息, Name 为 npm-test,然后 Save

Untitled 2.png

当当当当!

Untitled 3.png

4. 创建 Repository

接下来,选中侧边栏的 Repositories

Untitled 4.png

然后 Create repository ,这里需要关心的是 npm(hosted)npm(proxy)npm(group)。

Untitled 5.png

4.1 创建 npm(hosted)

这里先选择 npm(hosted),后续 npm 发布本地包到这里。填写 NameBlob store 选择 3 中创建的 npm-test。

Untitled 6.png

Deployment policy 选择 Disable redeploy,然后点 Create repository

Untitled 7.png

4.2 创建 npm(proxy)

接下来就是 npm(proxy),顾名思义,是一层代理。填入 Name,配置 Remote storage registry.npmmirror.com

Untitled 8.png

Blob store 选择 3 中创建的 npm-test。

Untitled 9.png

再点 Create repository

Untitled 10.png

4.3 创建 npm(group)

然后 npm(group),填写 NameBlob store 选择 3 中创建的 npm-test。

Untitled 11.png

将前面创建的 npm-hosted 与 npm-proxy 依次添加进来,次序也是优先级,点 Create repository

Untitled 12.png

至此,完成了 npm 私库的 Repositories创建。找到对应的 npm-group 点击 copy 按钮,即是我们的私库源地址。

Untitled 13.png

5. 验证

5.1 依赖安装测试

找个项目测试一下,先删除掉 node_modules,设置下安装源。

npm config get registry
# https://registry.npmjs.org/

npm config set registry http://192.168.2.159:8081/repository/npm-group

再 npm install 一下,可以看到 node_modules 又回来了。

(base) ➜  project-foo npm install
.....
added 27 packages from 48 contributors in 13.576s

2 packages are looking for funding
  run `npm fund` for details

(base) ➜  project-foo ls -l
total 72
-rw-r--r--   1 youngx  staff    631  5  9 10:37 README.md
-rw-r--r--   1 youngx  staff    331  5  9 10:37 index.html
drwxr-xr-x  21 youngx  staff    672  5 10 17:10 node_modules
-rw-r--r--   1 youngx  staff  17891  5 10 17:10 package-lock.json
-rw-r--r--   1 youngx  staff    292  5  9 10:37 package.json
drwxr-xr-x   3 youngx  staff     96  5  9 10:37 public
drwxr-xr-x   6 youngx  staff    192  5  9 10:37 src
-rw-r--r--   1 youngx  staff    300  5  9 10:37 vite.config.js

5.2 nexus Browse 查看

我们再打开 npm-group 就会发现有缓存,可以对比下,测试之前的空空如也。

Untitled 14.png

测试完之后的的,下载好的依赖包缓存。

Untitled 15.png

下一篇 nexus 的 npm 私库应用(用户) ,一起试验下发布 npm 包至私库。

6. 问题记录

如果发现配置上去的 npm-proxy 访问 404 的话,将 Support —> Logging 下的 org.apache.http 的 Logger Level 置为 DEBUG

Untitled 16.png

重新 npm install 之后,就会有 Logs,有问题的话就可以进一步排查了。

Untitled 17.png

7. 参考文档

淘宝 NPM 镜像站切换新域名啦