萬物根源-一分鐘教你釋出npm包

安穩.發表於2019-03-08

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、增加新特性,可向後相容,增加y
  3、有很大的改動,無法向下相容,增加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包就此釋出好了

萬物根源-一分鐘教你釋出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-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

#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$ 
複製程式碼

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-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$ 
複製程式碼

如果你覺得我的文章還不錯的話,可以給個star哦~,GitHub地址

相關文章