如何發佈 npm 包
這裏藉助 webpack 官網中的一個 demo (opens new window) 來演示如何發佈一個 npm 包。
# 註冊 npm 賬號
# 填寫註冊信息
登錄官網,點擊 Join
註冊賬戶,填寫 Full Name
、Public Email
、Username
、Password
信息,點擊 Create an Account
按鈕進行註冊。
註冊後,Username
將無法進行修改,請認真起名字~
# 驗證郵箱
註冊後,npm 網站會出現一個需要驗證郵箱的提示信息。
同時,註冊郵箱會收到一封驗證郵件。如果沒有驗證郵箱地址,將無法發佈 npm 包。
# 修改 npm 默認頭像(可選)
這一步無關緊要,你可以點擊此處跳過,進入下一步~
npm 使用 Gravatar (opens new window) 頭像,這裏可以將 npm 頭像設置爲自定義頭像。
- 點擊頁面右上角頭像,點擊
Profile Settings
,進入個人信息設置頁面
-
點擊
🖼 Change your Gravatar
進入gravatar
網站 -
點擊
Add email address
,輸入註冊 npm 賬號的郵箱,然後點擊Add
Add a new image
增加頭像- 點擊新增的頭像,
Pick new image
爲其選擇頭像
npm 網站頭像同步略有延遲
# 設置鏡像源
npm config list
查看 npm 配置信息。如果發現 registry
不是官方源 http://registry.npmjs.org
,請將倉庫鏡像源設置成官方鏡像: npm config set registry=http://registry.npmjs.org
。 否則發佈時會出現 npm ERR! publish Failed PUT 403
。
wangfuchengdeMac-mini:webpack-numbers wangfucheng$ npm publish
npm notice
npm notice 📦 webpack-numbers-demo@1.0.0
npm notice === Tarball Contents ===
npm notice 357B package.json
npm notice 94B README.md
npm notice 435B webpack.config.js
npm notice 1.7kB dist/webpack-numbers.js
npm notice 622B example/browser/index.html
npm notice 218B example/node/index.js
npm notice 678B src/index.js
npm notice 254B src/ref.json
npm notice === Tarball Details ===
npm notice name: webpack-numbers-demo
npm notice version: 1.0.0
npm notice package size: 2.2 kB
npm notice unpacked size: 4.4 kB
npm notice shasum: d4c8eeedcdfd8740d86cbff0e7406240afcb927b
npm notice integrity: sha512-C9dqKauWLpHtd[...]fZQsfi1tZ86aQ==
npm notice total files: 8
npm notice
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! [no_perms] Private mode enable, only admin can publish this module [no_perms] Private mode enable, only admin can publish this module: webpack-numbers-demo
# 本地終端登錄 npm
本地試用 npm adduser
登錄 npm
wangfuchengdeMac-mini:webpack-numbers wangfucheng$ npm adduser
Username: anonymity94
Password:
Email: (this IS public) 1176989591@qq.com
Logged in as anonymity94 on http://registry.npmjs.org/.
可以使用 npm config list
查看 npm 登錄信息
wangfuchengdeMac-mini:webpack-numbers wangfucheng$ npm config list
; cli configs
metrics-registry = "http://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.4.1 node/v10.15.2 darwin x64"
; userconfig /Users/wangfucheng/.npmrc
//registry.npm.taobao.org/:always-auth = false
//registry.npm.taobao.org/:email = "1176989591@qq.com"
//registry.npm.taobao.org/:username = "anonymity94"
registry = "http://registry.npmjs.org/"
; node bin location = /Users/wangfucheng/.nvm/versions/node/v10.15.2/bin/node
; cwd = /Users/wangfucheng/coading/webpack-numbers
; HOME = /Users/wangfucheng
; "npm config ls -l" to show all defaults.
# 發佈 npm 包
# 確認包名稱和版本
確認 package.json
中的 name
和 version
字段。
當要發佈的包名和 npm 上的包重名時會有錯誤產生,所以可以先去 npm 官網上搜索一下,名字是否已被使用。
發佈包的名字要保證是獨一無二的。當重名時會有如下錯誤:
wangfuchengdeMac-mini:webpack-numbers wangfucheng$ npm publish
npm notice
npm notice 📦 webpack-numbers-demo@1.0.0
npm notice === Tarball Contents ===
npm notice 357B package.json
npm notice 94B README.md
npm notice 435B webpack.config.js
npm notice 1.7kB dist/webpack-numbers.js
npm notice 622B example/browser/index.html
npm notice 218B example/node/index.js
npm notice 678B src/index.js
npm notice 254B src/ref.json
npm notice === Tarball Details ===
npm notice name: webpack-numbers-demo
npm notice version: 1.0.0
npm notice package size: 2.2 kB
npm notice unpacked size: 4.4 kB
npm notice shasum: d4c8eeedcdfd8740d86cbff0e7406240afcb927b
npm notice integrity: sha512-C9dqKauWLpHtd[...]fZQsfi1tZ86aQ==
npm notice total files: 8
npm notice
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You do not have permission to publish "webpack-numbers-demo". Are you logged in as the correct user? : webpack-numbers-demo
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/wangfucheng/.npm/_logs/2019-09-04T08_34_20_996Z-debug.log
# 發佈
使用 npm publish
命令進行發佈。
wangfuchengdeMac-mini:webpack-numbers wangfucheng$ npm publish
npm notice
npm notice 📦 webpack-numbers-publish-demo@1.0.0
npm notice === Tarball Contents ===
npm notice 365B package.json
npm notice 94B README.md
npm notice 435B webpack.config.js
npm notice 1.7kB dist/webpack-numbers.js
npm notice 622B example/browser/index.html
npm notice 218B example/node/index.js
npm notice 678B src/index.js
npm notice 254B src/ref.json
npm notice === Tarball Details ===
npm notice name: webpack-numbers-publish-demo
npm notice version: 1.0.0
npm notice package size: 2.2 kB
npm notice unpacked size: 4.4 kB
npm notice shasum: cbe951478422f1ec93d25bd9f675e57741322ad9
npm notice integrity: sha512-n7nVwkFJrAVlB[...]PMWQNDRdmGpZA==
npm notice total files: 8
npm notice
+ webpack-numbers-publish-demo@1.0.0
發佈成功後,可以在 npm 倉庫中搜索到。
# 刪除 npm 包
已經發布的包,只允許在 72 小時內撤銷。
https://docs.npmjs.com/unpublishing-packages-from-the-registry (opens new window)
In order to permanently remove a package (or package version) from the npm registry, as a package owner or collaborator, you can unpublish it from the the command line within 72 hours of the initial publish.
刪除命令:npm unpublish --force
。
wangfuchengdeMac-mini:webpack-numbers wangfucheng$ npm unpublish --force
npm WARN using --force I sure hope you know what you are doing.
- webpack-numbers-publish-demo@1.0.0
刪除後,npm 倉庫中已經找不到相關的包。
# ⚠️ 錯誤總結
- 鏡像不是官方鏡像
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! [no_perms] Private mode enable, only admin can publish this module [no_perms] Private mode enable, only admin can publish
- 郵箱未驗證
npm ERR! You must verify your email before publishing a new package: https://www.npmjs.com/email-edit : cuitaonpm
- 賬戶未登錄
npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
npm ERR! need auth You need to authorize this machine using `npm adduser`
- 包名已經被使用
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You do not have permission to publish "webpack-numbers-demo". Are you logged in as the correct user? : webpack-numbers-demo
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:http://anonymity94.github.io/articles/how-to-publish-a-npm-package/#%E6%B3%A8%E5%86%8C-npm-%E8%B4%A6%E5%8F%B7