文章简介: 一、摘要:什么是npm? 二、如何发布一个本身的npm包 三、发布错误集锦javascript
npm是javascript著名的包管理工具,是前端模块化下的一个标志性产物 简单地地说,就是经过npm下载模块,复用已有的代码,提升工做效率 和移动端开发中,iOS使用的Cocoapods,Android使用的maven有殊途同归之妙。前端
发布包以前你必需要注册一个npm的帐号java
a、本地建立一个文件夹:例如:z-tool b、执行命令进入目录: $ cd z-tool c、执行npm init 初始化项目。默认一路回车就行git
sh-neverleave:z-tool neverleave$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (z-tool)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/neverleave/Desktop/z-tool/package.json:
{
"name": "z-tool",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes)
复制代码
默认字段简介:
name:发布的包名,默认是上级文件夹名。不得与如今npm中的包名重复。包名不能有大写字母/空格/下滑线!
version:你这个包的版本,默认是1.0.0。对于npm包的版本号有着一系列的规则,模块的版本号采用X.Y.Z的格式,具体体现为:
一、修复bug,小改动,增长z。
二、增长新特性,可向后兼容,增长y
三、有很大的改动,没法向下兼容,增长x
description:项目简介
mian:入口文件,默认是Index.js,能够修改为本身的文件
scripts:包含各类脚本执行命令
test:测试命令。
author:写本身的帐号名
license:这个直接回车,开源文件协议吧,也能够是MIT,看须要吧。
复制代码
d、在z-tool文件夹中建立一个文件名为index.js的文件,简单的写了一下内容。npm
!function(){
console.log(`这是引入的包入口`)
}()
复制代码
在终端输入npm adduser,提示输入帐号,密码和邮箱,而后将提示建立成功,具体以下图。 【注意】npm adduser成功的时候默认你已经登录了,因此可跳过第四步。 json
在终端输入npm login,而后输入你建立的帐号和密码,和邮箱,登录,结果同步骤三。segmentfault
成功发布:bash
sh-neverleave:z-tool neverleave$ npm publish
+ z-tool@1.0.1
复制代码
注意:若是项目里有部分私密的代码不想发布到npm上,能够将它写入.gitignore 或.npmignore中,上传就会被忽略了微信
到npm官网全局搜索便可maven
和其余包使用方式一致,具体使用能够看源码介绍或者README.md。
终端执行 npm unpublish 例如: 一、npm unpublish z-tool@1.0.0 删除某个版本 二、npm unpublish z-tool --force 删除整个npm市场的包
不过撤包推荐用法: npm unpublish的推荐替代命令:npm deprecate [@] 使用这个命令,并不会在社区里撤销你已有的包,但会在任何人尝试安装这个包的时候获得警告 例如:npm deprecate z-tool '这个包我已经再也不维护了哟~'
【注意】若是报权限方面的错,加上--force
一、须要提升版本号
#一、发包 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
#二、发包 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$
复制代码
二、发包 npm publish 失败 解决方案:终端执行: npm publish --access public
参考:stackoverflow.com/questions/5…
#一、发包 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$
复制代码
三、确保登陆的用户帐号正确
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
复制代码
四、登陆时须要在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$
复制代码
五、无权限删除线上的包(撤包有时间限制,24小时) 解决方案:加上 --force
sh-neverleave:z-tool neverleave$ npm unpublish z-tool
npm ERR! Refusing to delete entire project.
npm ERR! Run with --force to do this.
npm ERR! npm unpublish [<@scope>/]<pkg>[@<version>]
sh-neverleave:z-tool neverleave$
#解决方案(内部有被鄙视的话,😄 I sure hope you know what you are doing.)
sh-neverleave:z-tool neverleave$ npm unpublish z-tool --force
npm WARN using --force I sure hope you know what you are doing.
- z-tool
sh-neverleave:z-tool neverleave$
复制代码
六、删除npm市场的包同名的24小时后才能从新发布
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! z-tool cannot be republished until 24 hours have passed. : z-tool
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T11_41_24_086Z-debug.log
sh-neverleave:z-tool neverleave$
复制代码
完结
关注微信公众号,提高综合技能