封装 npm 包上传到 npmjs.com

定义 package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"name": "logger-server", // 会被当作包名存在,不可与现有 npm 仓库的包名重复(例如已经有了 log4js,这里就不能叫 log4js)
"version": "1.0.0", // 打版本标签
"private": true, // 这个字段可以防止代码被公开发布
"description": "日志自定义封装",
"main": "index.js",
"author": "xiaowu",
"repository": { // 可不配
"type": "git",
"url": "git@xxx.git"
},
"license": "ISC",
"dependencies": {
"log4js": "^6.3.0"
}
}

发布和管理

1
2
npm publish                   # 上传
npm unpublish --force # 下架(24小时内不能重复上传当前包名的 npm 包)

然后登录 https://www.npmjs.com/ 查看

使用

1
"logger-server": "^1.0.0"

封装 npm 包上传到自建仓库(GitLab 等)

提交到仓库

使用

1
2
3
4
"@组/包名": "git+http://[账户:密码@(如果是public则删除此部分)]xxx.git#{branch|tag}"  // http(s)方式,版本号不加则使用最新版本
"@组/包名": "git+ssh://git@xxx.git#{branch|tag}" // ssh方式(不可忘了配密钥)

## 如果不加 branch 或者 tag 则使用 master 分支代码

单独更新自定义 npm 包

1
npm update @组/包名