记录一下发布过程中遇到的问题
封装自己的 npm 包
具体封装过程就不写了,注意配置好 package.json,具体如下:
{
"name": "yourpkgname",
"version": "0.1.0",
"description": "description",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "./node_modules/.bin/babel src --out-dir dist", // 将源码编译到dist文件夹,入口文件也要改成dist/index.js
"prepublish": "npm run compile" // npm publish之前会prepublish
},
"repository": {
"type": "git", "url": "https://github.com/authorname/reponame.git"
},
"bugs": {
"url": "https://github.com/authorname/reponame/issues"
},
"homepage": "https://github.com/authorname/reponame#readme",
"author": "authorname",
"license": "MIT",
"devDependencies": { // 你的依赖
},
"dependencies": { // 你的依赖
}
}
注册 npm 账号
在 https://www.npmjs.com 上面注册一个账号,发布Public Packages,免费用户即可,要想发布 Private Packages,需要$7/User
注册号后一定要去邮箱激活
登录账户
切换到本地 npm 包目录,登录 npm 账户
npm adduser
username:
password:
email:
依次输入用户名,密码和邮箱,登录成功后可以查看当前登录用户
npm whoami
发布 npm 包
npm publish
发布成功后,可以在命令行内看到
+ yourpkgname@0.1.0
表示已经发布成功,可以去 https://npmjs/packages/yourpkgname 查看你刚才发布的包
更新包
先修改本地 package.json 里的 version,再执行发布命令npm publish
使用你的npm包
在新的项目里面npm i –save yourpkgname,就可以愉快的使用了。
发布遇到的问题
1、登录失败
删除项目目录内的.npmrc文件,重新执行npm adduser
2、发布没有权限
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You do not have permission to publish "your-package". Are you logged in as the correct user? : your-package
你的包名可能和别人的重名了,去npm里搜一下,如果重复改一个名字重新发布

