nexus 的 npm 私库应用(用户)

832 阅读3分钟

nexus 的 npm 私库应用(用户)

1. 背景介绍

哈喽,大家好啊!在介绍完 nexus 的 npm 私库应用(Repository 创建) 之后呢,不禁想到如何把自己心爱的 npm 包发布至私库呢?带着这样的疑问和思考,下面为大家操作一番。

2. 创建用户

选择侧边栏中的 Users ,点击 Ctreate local user

Untitled.png

啊。。一口气需要填写这么多的必填字段之后,注意 Status 一定要是 Active !(好像有点怪哈)。

Untitled 1.png

赋予其 nx-adminnx-anonymous 角色,当然还可以创建你喜欢的角色在侧边栏的 Roles,这样一个亲切友好的用户 paxtonx 就创建好了。Ctreate local user 一下。

Untitled 2.png

3. 验证

3.1 准备 npm 测试包

下面让我们试试看,准备一个 npm 包 npm-publish,因只用作测试使用,只需 npm init 一下。

(base) ➜  npm-publish npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (npm-publish)
version: (1.0.0)
description: test
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/youngx/WebstormProjects-foo/npm-publish/package.json:

{
  "name": "npm-publish",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes) yes

3.2 登录

登录到 npm 私库,注意是 npm-hosted 的地址哦,输入用户名、密码、邮箱信息。

(base) ➜  npm-publish npm login --registry=http://192.168.2.159:8081/repository/npm-hosted/
Username: paxtonx
Password:
Email: (this IS public) paxtonx@qq.com
Logged in as paxtonx on http://192.168.2.159:8081/repository/npm-hosted/.

3.3 发布

发布到 npm 私库。

(base) ➜  npm-publish npm publish --registry=http://192.168.2.159:8081/repository/npm-hosted/
npm notice
npm notice 📦  npm-publish@1.0.0
npm notice === Tarball Contents ===
npm notice 211B package.json
npm notice === Tarball Details ===
npm notice name:          npm-publish
npm notice version:       1.0.0
npm notice package size:  245 B
npm notice unpacked size: 211 B
npm notice shasum:        19a2068412fea5c2549bb2d943fd834df14225f1
npm notice integrity:     sha512-d0bXXeDD9+36t[...]jIfcIY8Km4epw==
npm notice total files:   1
npm notice
npm ERR! code E401
npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/youngx/.npm/_logs/2023-05-10T10_34_03_160Z-debug.log

3.4 常见问题

出现这种错误信息,不要慌。是需要设置 Realms 选中 npm Bearer Token RealmSave

Untitled 3.png

并将 npm-hosted 的 Deployment policy 设置为 Allow redeploy, Save

Untitled 4.png

3.5 成功

调整之后进行保存,再次发布下。阳光总在风雨后,古人诚不欺我。

(base) ➜  npm-publish npm publish --registry=http://192.168.2.159:8081/repository/npm-hosted/
npm notice
npm notice 📦  npm-publish@0.0.1
npm notice === Tarball Contents ===
npm notice 211B package.json
npm notice === Tarball Details ===
npm notice name:          npm-publish
npm notice version:       0.0.1
npm notice package size:  245 B
npm notice unpacked size: 211 B
npm notice shasum:        ef552e29bba1c0000ec12b7bf924fae585a02821
npm notice integrity:     sha512-ltBrLpcwZ+Xqt[...]zXtxNMFBF20ww==
npm notice total files:   1
npm notice
+ npm-publish@0.0.1

进入到 Browse 发现,测试成功!

Untitled 5.png

4. 结语

至此,nexus 的探索之旅告一段落了。我们进行了一些简单的操作,如安装、Repository 创建、用户配置并验证,当然,还有很多的功能模块需要大家花点时间慢慢摸索尝试。如果你也想使用 nexus 的话,那么 nexus 搭建 npm 私库 对你有帮助的话,将会是我莫大的荣幸。