发布自己的NPM包
1 注册自己的 npm 账号
- 进入 npm 官网注册账号
2 登录账号
npm login
3 安装 nrm
npm install nrm -g
nrm ls
nrm use npm
4 创建项目文件夹
mkdir js-tools && cd js-tools
5 npm 初始化
npm init -y
6 配置 package.json
{
"name": "wxdxjs-tools",
"version": "1.0.0",
"description": "this is my first package",
"main": "index.js",
"private": false,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "chenwei",
"license": "ISC"
}
7 创建 README.md
this is my package
8 创建 index.js
module.exports = {
printMsg: function () {
console.log('this message is from yuyy-test-pkg!');
}
}
目录结构
── js-tools
├── README.md
├── index.js
└── package.json
9 npm 发布
npm publish
10 版本迭代
❝需要修改 package.json 的版本号,每次根据需求增加
❞
+ "version": "1.0.0"
- "version": "1.0.1",
11 npm 取消发布
npm unpublish wxdxjs-tools --force
可能报的错
code E402
402 Payment Required - PUT
http://registry.npmjs.org/@dinghx%2funifiedValidator
- You must sign up for private packages
这是因为包名是 @xxx/xxxx npm会认为要发布私包,私包需要收费,需将发布命令改成
npm publish --access public
code E403
code E403 没权限发布
npm ERR! [no_perms] Private mode enable, only admin can
publish this module [no_perms]
Private mode enable, only admin can publish
this module: your-package
这个是你的源设置成第三方源的时候才有可能发生,比如设置了淘宝源就可能会导致该问题。只要把源改回默认的就可以了,如下
npm config set registry http://registry.npmjs.org
包名相似
npm ERR! Package name too similar to existing packages;
try renaming your package to '@dinghx/captcha' and publishing
with 'npm publish --access=public' instead : your-package
邮箱没有校验
npm ERR! you must verify your email before publishing a new package
: https://www.npmjs.com/email-edit : your-package
这是邮箱没有校验,登录https://www.npmjs.com 重新发起邮箱验证
Done