本文讲解从零开始使用 Verdacc.io 搭建私有 NPM 包的完整流程。
目录
1. Verdacc.io 简介
Verdacc.io 是免费的私有 NPM 包托管服务。
1.1 与公区 NPM 对比
| 特性 | 公区 NPM | Verdacc.io |
|---|---|---|
| 洁用 | 每个包收费 | 免费 |
| 可见性 | 所有人可见 | 需要登录访问 |
| 访问速度 | CDN 快 | 相对较慢 |
| 适用场景 | 开源项目 | 私有项目 |
1.2 适用场景
- 个人私有工具包
- 公司内部组件库
- 不想公开的私有代码
- NPM 包名已被占用
2. 创建项目
2.1 项目初始化
mkdir my-private-package
cd my-private-package
npm init -y
2.2 配置 package.json
{
"name": "@your-username/your-package",
"version": "1.0.0",
"description": "我的私有工具包",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "node --test",
"build": "tsc"
},
"keywords": ["utility", "helper"],
"author": "your-name",
"license": "MIT",
"publishConfig": {
"access": "restricted"
}
}
重要配置:
name: 使用 scope,格式为@username/package-namepublishConfig.access: 设置为restricted表示私有包main: 入口文件路径types: TypeScript 类型定义文件
2.3 创建入口文件
// index.js
function add(a, b) {
return a + b
}
function multiply(a, b) {
return a * b
}
module.exports = {
add,
multiply
}
// index.d.ts
export function add(a: number, b: number): number;
export function multiply(a: number, b: number): number;
3. 注册账号
3.1 注册步骤
- 访问 https://verdacc.io
- 点击 “Sign Up” 注册
- 使用邮箱或 GitHub 账号注册
- 完成邮箱验证
3.2 创建 Scope(命名空间)
Scope 是 Verdacc.io 的命名空间,确保你的包名唯一。
Scope 命名规则:
- 只能包含小写字母、数字、连字符、点
- 长度 2-214 个字符
- 不能以
.开头或结尾
示例:
- ✅
my-username - ✅
my-company - ❌
My-Username(不能大写) - ❌
my_username(不能有下划线)
创建步骤:
- 登录 Verdacc.io 后进入个人页面
- 点击 “New scope” 创建新的命名空间
- 输入 scope 名称
4. 配置 NPM 源
4.1 登录 Verdacc.io
npm login --registry=https://registry.verdacc.io
输入用户名和密码后,会保存认证信息到 ~/.npmrc 文件。
4.2 设置项目级配置(推荐)
在项目根目录创建 .npmrc 文件:
registry=https://registry.verdacc.io
4.3 配置全局默认源
编辑 ~/.npmrc 文件:
registry=https://registry.npmjs.org
@your-username:registry=https://registry.verdacc.io
效果:
- 默认使用公区 NPM
@your-username/xxx开头的包自动使用 Verdacc.io
5. 发布包
5.1 发布命令
npm publish --registry=https://registry.verdacc.io
5.2 发布流程详解
1. npm 检查 package.json
↓
2. 打包文件为 tarball
↓
3. 上传到 Verdacc.io registry
↓
4. 生成 package.json(发布后)
↓
5. 发布成功,显示包信息
5.3 验证发布
访问以下 URL 验证包是否发布成功:
https://www.verdacc.io/packages/@your-username/your-package
6. 使用包
6.1 在新项目中安装
# 安装私有包
npm install @your-username/your-package --registry=https://registry.verdacc.io
# 或配置 .npmrc 后直接安装
npm install @your-username/your-package
6.2 在代码中使用
// CommonJS
const { add, multiply } = require('@your-username/your-package')
console.log(add(1, 2)) // 3
console.log(multiply(2, 3)) // 6
// TypeScript
import { add, multiply } from '@your-username/your-package'
console.log(add(1, 2)) // 3
console.log(multiply(2, 3)) // 6
// Vue 项目
<script setup>
import { add } from '@your-username/your-package'
const result = add(1, 2)
</script>
6.3 版本更新
更新 package.json 中的版本号:
{
"version": "1.1.0"
}
然后重新发布:
npm publish --registry=https://registry.verdacc.io
使用时指定版本:
npm install @your-username/your-package@1.0.0 npm install @your-username/your-package@1.1.0
总结
| 步骤 | 操作 | 命令 |
|---|---|---|
| 1 | 创建项目 | npm init -y |
| 2 | 配置 package.json | 编辑文件 |
| 3 | 注册 Verdacc.io | 访问网站 |
| 4 | 创建 Scope | 在网站创建 |
| 5 | 配置 NPM 源 | .npmrc 或 npm adduser |
| 6 | 登录 | npm login |
| 7 | 发布包 | npm publish |
| 8 | 安装使用 | npm install |
完整流程:
创建项目 → 配置 package.json → 注册账号 → 配置 NPM 源 → npm login → npm publish → 验证发布 → 安装使用
通过 Verdacc.io,你可以方便地管理私有 NPM 包,适合个人或小团队使用。