一分鐘教你發(fā)布npm包

文章簡(jiǎn)介:
1、摘要:什么是npm?
2、如何發(fā)布一個(gè)自己的npm包
3、發(fā)布錯(cuò)誤集錦

摘要:什么是npm?

npm是javascript著名的包管理工具,是前端模塊化下的一個(gè)標(biāo)志性產(chǎn)物
簡(jiǎn)單地地說,就是通過npm下載模塊,復(fù)用已有的代碼,提高工作效率
和移動(dòng)端開發(fā)中,iOS使用的Cocoapods,Android使用的maven有異曲同工之妙。

如何發(fā)布一個(gè)自己的npm包

1、創(chuàng)建一個(gè)npm的賬號(hào)

發(fā)布包之前你必須要注冊(cè)一個(gè)npm的賬號(hào)

2、初始化一個(gè)簡(jiǎn)單的項(xiàng)目發(fā)布

a、本地創(chuàng)建一個(gè)文件夾:例如:z-tool
b、執(zhí)行命令進(jìn)入目錄: $ cd z-tool
c、執(zhí)行npm init 初始化項(xiàng)目。默認(rèn)一路回車就行

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) 

默認(rèn)字段簡(jiǎn)介:
name:發(fā)布的包名,默認(rèn)是上級(jí)文件夾名。不得與現(xiàn)在npm中的包名重復(fù)。包名不能有大寫字母/空格/下滑線!
version:你這個(gè)包的版本,默認(rèn)是1.0.0。對(duì)于npm包的版本號(hào)有著一系列的規(guī)則,模塊的版本號(hào)采用X.Y.Z的格式,具體體現(xiàn)為:
  1、修復(fù)bug,小改動(dòng),增加z。
  2、增加新特性,可向后兼容,增加y
  3、有很大的改動(dòng),無(wú)法向下兼容,增加x
description:項(xiàng)目簡(jiǎn)介
mian:入口文件,默認(rèn)是Index.js,可以修改成自己的文件 
scripts:包含各種腳本執(zhí)行命令
test:測(cè)試命令。
author:寫自己的賬號(hào)名
license:這個(gè)直接回車,開源文件協(xié)議吧,也可以是MIT,看需要吧。

d、在z-tool文件夾中創(chuàng)建一個(gè)文件名為index.js的文件,簡(jiǎn)單的寫了一下內(nèi)容。

!function(){
  console.log(`這是引入的包入口`)
}()
3、如果本機(jī)第一次發(fā)布包(非第一次可忽略)

在終端輸入npm adduser,提示輸入賬號(hào),密碼和郵箱,然后將提示創(chuàng)建成功,具體如下圖。
【注意】npm adduser成功的時(shí)候默認(rèn)你已經(jīng)登陸了,所以可跳過第四步。

npm adduser 執(zhí)行結(jié)果

最后一行顯示登錄信息,as 后面是用戶名。on 后是源地址,如果不是https://registry.npmjs.org/,比如是淘寶源,請(qǐng)切換??梢詤⒖迹?a target="_blank">https://segmentfault.com/a/1190000004444283

4、非第一次發(fā)布包

在終端輸入npm login,然后輸入你創(chuàng)建的賬號(hào)和密碼,和郵箱,登陸,結(jié)果同步驟三。

5、npm publish 發(fā)包

成功發(fā)布:

sh-neverleave:z-tool neverleave$ npm publish
+ z-tool@1.0.1

注意:如果項(xiàng)目里有部分私密的代碼不想發(fā)布到npm上,可以將它寫入.gitignore 或.npmignore中,上傳就會(huì)被忽略了

6、查詢發(fā)布的包

到npm官網(wǎng)全局搜索即可

7、安裝使用方式

和其他包使用方式一致,具體使用可以看源碼介紹或者README.md。

8、如何撤銷發(fā)布的包

終端執(zhí)行 npm unpublish
例如:
1、npm unpublish z-tool@1.0.0 刪除某個(gè)版本
2、npm unpublish z-tool --force 刪除整個(gè)npm市場(chǎng)的包

不過撤包推薦用法:
npm unpublish的推薦替代命令:npm deprecate <pkg>[@<version>] <message>
使用這個(gè)命令,并不會(huì)在社區(qū)里撤銷你已有的包,但會(huì)在任何人嘗試安裝這個(gè)包的時(shí)候得到警告
例如:npm deprecate z-tool '這個(gè)包我已經(jīng)不再維護(hù)了喲~'

【注意】如果報(bào)權(quán)限方面的錯(cuò),加上--force

發(fā)布錯(cuò)誤集錦

1、需要提高版本號(hào)

#1、發(fā)包 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、發(fā)包 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、發(fā)包 npm publish 失敗
解決方案:終端執(zhí)行: npm publish --access public

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

#1、發(fā)包 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$ 

#解決方案:終端執(zhí)行: npm publish --access public
sh-neverleave:npm neverleave$ npm publish --access public
+ z-tool@1.0.0
sh-neverleave:npm neverleave$ 

3、確保登錄的用戶賬號(hào)正確

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、登錄時(shí)需要在username 前加‘~’,具體大家可以驗(yàn)證

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$ 

5、無(wú)權(quán)限刪除線上的包(撤包有時(shí)間限制,24小時(shí))
解決方案:加上 --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$ 

#解決方案(內(nèi)部有被鄙視的話,?? 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$ 

6、刪除npm市場(chǎng)的包同名的24小時(shí)后才能重新發(fā)布

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$ 

完結(jié)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容