万物根源-一分钟教你发布npm包
Created By JishuBao on 2019-03-08 12:38:22
Recently revised in 2019-03-08 12:38:22
?
??歡迎大家來到技術寶的掘金世界,您的star是我寫文章最大的動力!GitHub地址 ?? ?
文章簡介:
1、摘要:什么是npm?
2、如何發布一個自己的npm包?
3、發布錯誤集錦
一、摘要:什么是npm?
?npm是javascript著名的包管理工具,是前端模塊化下的一個標志性產物。簡單來說,就是通過npm下載模塊,復用已有的代碼,提高工作效率
二、如何發布一個屬于自己的npm包
1、創建一個npm賬號
?在這里 npm注冊賬號地址,用戶名賬號密碼郵箱注冊完后,驗證以下郵箱即可。
2、初始化一個簡單的項目發布
a.本地創建一個文件夾:例如JishuBao
b.執行命令進入目錄:$ cd JishuBao
c.執行npm init 初始化項目。默認一路回車就行。
$ 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: (jishubao) version: (1.0.0) description: a test npm publish entry point: (index.js) test command: git repository: keywords: author: jishubao license: (ISC) About to write to E:\MyProject\JishuBao\package.json:{"name": "jishubao","version": "1.0.0","description": "a test npm publish","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "jishubao","license": "ISC" }Is this ok? (yes) y 復制代碼默認字段簡介: name:發布的包名,默認是上級文件夾名。不得與現在npm中的包名重復。包名不能有大寫字母/空格/下滑線! version:你這個包的版本,默認是1.0.0。對于npm包的版本號有著一系列的規則,模塊的版本號采用X.Y.Z的格式,具體體現為:1、修復bug,小改動,增加z。2、增加新特性,可向后兼容,增加y3、有很大的改動,無法向下兼容,增加x description:項目簡介 mian:入口文件,默認是Index.js,可以修改成自己的文件 scripts:包含各種腳本執行命令 test:測試命令。 author:寫自己的賬號名 license:這個直接回車,開源文件協議吧,也可以是MIT,看需要吧。 復制代碼d.在JishuBao文件夾中創建一個名為index.js的文件,簡單的寫了一下內容。
!function(){console.log('這是技術寶測試發布的npm包'); }() 復制代碼3、如果本機第一次發布包(非第一次可忽略);
在終端輸入npm adduser,提示輸入賬號,密碼和郵箱,然后將提示創建成功,具體如下圖。
【注意】npm adduser成功的時候默認你已經登陸了,所以可跳過第四步。
$ npm adduser Username: user.name(自己注冊的用戶名) Password: user.password(自己注冊的密碼) Email: (this IS public) 2459666474@qq.com(自己的郵箱) Logged in as wjb666 on https://registry.npmjs.org/.(成功提示)復制代碼最后一行顯示登錄信息,as 后面是用戶名。on 后是源地址
4、非第一次發包
?在終端輸入npm login,然后輸入你創建的賬號和密碼,和郵箱,登陸,結果同步驟三。
5、npm publish 發布包
成功發布:
$ npm publish + jishubao@1.0.0 復制代碼注意:如果項目里有部分私密的代碼不想發布到npm上,可以將它寫入.gitignore 或.npmignore中,上傳就會被忽略了
6、查詢發布的包
到npm官網全局搜索即可,如下圖jishubao的npm包就此發布好了
7、安裝使用方式
和其他包使用方式一致。將剛才的文件夾清空。
$ npm install jishubao --save-dev npm WARN saveError ENOENT: no such file or directory, open 'E:\MyProject\JishuBao\package.json' npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN enoent ENOENT: no such file or directory, open 'E:\MyProject\JishuBao\package.json' npm WARN JishuBao No description npm WARN JishuBao No repository field. npm WARN JishuBao No README data npm WARN JishuBao No license field.+ jishubao@1.0.0 added 1 package in 1.577s復制代碼8、如何撤銷發布的包
終端執行 npm unpublish
例如:
1、npm unpublish z-tool@1.0.0 刪除某個版本
2、npm unpublish z-tool --force 刪除整個npm市場的包
不過撤包推薦用法:
npm unpublish的推薦替代命令:npm deprecate[@] 使用這個命令,并不會在社區里撤銷你已有的包,但會在任何人嘗試安裝這個包的時候得到警告 例如:npm deprecate z-tool '這個包我已經不再維護了喲~
【注意】如果報權限方面的錯,加上--force
三、錯誤集錦
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-toolnpm 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-toolnpm 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
#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-toolnpm 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: Basicnpm 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、無權限刪除線上的包(撤包有時間限制,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$ 復制代碼6、刪除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-toolnpm 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$ 復制代碼如果你覺得我的文章還不錯的話,可以給個star哦~,GitHub地址
總結
以上是生活随笔為你收集整理的万物根源-一分钟教你发布npm包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于区块链交易技术开发的证券
- 下一篇: Windows配置jdk环境变量JAVA