发布npm包

170 阅读3分钟

1、注册账号

- 进入网址:https://www.npmjs.com 注册一个npm账号。
- 进邮箱验证npm账号(否则发布会报错)

2、创建一个简单的npm包

- 在本地创建一个文件夹,如:achirsh-demo(注意名称应该是npm上没有的,可以在npm上搜索一下看你命的名字是否存在)
- cd进入该文件夹
- 初始化项目,执行npm init -y得到package.json文件
- 在文件夹achirsh-demo中创建名为index.js的文件
- 创建README.md文件
// package.json

{
    "name": "achirsh-demo",  // 发布的报名,默认是上级文件夹。不得与现在npm中的包名重复。包名不能有大写字母/空格/下划线
    "version": "1.0.0",      // 默认版本号
    "description": "",       // 项目简介
    "main": "index.js",      // 入口文件,默认是index.js,可以修改成自己的文件
    "scripts": {             // 包含各种脚本执行命令
    	"test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",            // 作者
    "license": "ISC"         // 开源文件协议
}

3、发布npm

- 切换下载源,安装npm install -g nrm
  nrm ls         // 查看当前的下载源,带*指向的就是当前的下载源,如果为npm则不需要切换
  nrm use npm    // 切换到npm的下载源
- 连接npm账户
  1、如果是本地第一次安装npm包,在终端运行命令:npm addUser提示输入账号,密码和邮箱,然后将提示创建成功
  2、非第一次发布npm包,在终端输入npm login,然后输入你创建的账号和密码、邮箱,登陆
- 发布npm包
  npm build
- 更新npm发布后的包
  更新npm包也是使用npm publish命令发布,不过必须更改npm包的版本号,也就是package.json中的version字段,否则会报错
- 撤销命令
  npm unpublish 包名@版本号 --force
  eg: npm unpublish testtoast@1.0.0 --force
  说明:撤销发布的包被认为是一种不好的行为,因为如果有团队在使用和依赖你的包时,撤销便会造成很大的影响
  1、根据规范,只有在发包的24小时内才允许撤销发布的包
  2、即使你撤销了你发布的包,发包的时候也不能再和被撤销的包和版本重复了(即不能名称相同,版本相同,因为这两者构成的唯一标识已经被“占用”了)

4、可能遇到的错误

1、需要提高版本号

#1、发包 npm publish 失败
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! deprecations must be strings : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T10_52_01_742Z-debug.log
sh-neverleave:z-tool neverleave$ npm publish


#2、发包 npm publish 失败
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You cannot publish over the previously published versions: 1.0.3. : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T11_24_57_662Z-debug.log
sh-neverleave:z-tool neverleave$ 
2、发包 npm publish 失败
解决方案:终端执行: npm publish --access public

参考:https://stackoverflow.com/questions/53420758/npm-publish-gives-unscoped-packages-cannot-be-private

#1、发包 npm publish 失败
sh-neverleave:npm neverleave$ npm publish
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! unscoped packages cannot be private : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T08_44_21_310Z-debug.log
sh-neverleave:npm neverleave$ 

#解决方案:终端执行: npm publish --access public
sh-neverleave:npm neverleave$ npm publish --access public
+ z-tool@1.0.0
sh-neverleave:npm neverleave$ 
3、确保登录的用户账号正确

sh-neverleave:npm neverleave$ npm publish
npm ERR! publish Failed PUT 404
npm ERR! code E404
npm ERR! 404 User not found : z-tool
npm ERR! 404 
npm ERR! 404  'z-tool' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T07_32_28_518Z-debug.log
4、登录时需要在username 前加‘~’

sh-neverleave:npm neverleave$ npm login
Username: (~neverleave) neverleave
Password: (<default hidden>) 
Email: (this IS public) (1063588359@qq.com) 
npm ERR! code EAUTHIP
npm ERR! Unable to authenticate, need: Basic

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T07_27_50_877Z-debug.log
sh-neverleave:npm neverleave$