npm publish 發佈一個 Angular 庫的時候報錯以及解決方法

我今天照着 npm 文件,打算把自己開發的 npm 庫發佈到公網上:

npm login 之後,使用命名行 npm config set registry=http://registry.npmjs.org, 遇到如下錯誤:

npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://github.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/
npm ERR! code E426
npm ERR! 426 Upgrade Required - PUT http://registry.npmjs.org/my-lib

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\I042416\AppData\Roaming\npm-cache_logs\2021-10-17T02_40_15_408Z-debug.log

大意是,從2021年10月4日開始,所有前往 npm registry 的連接,都需要使用 TLS 1.2 了.

因此,我把命名行改成使用 https,舊的錯誤消息就消失了:

npm config set registry=http://registry.npmjs.org

然而我又遇到了新的錯誤消息:you do not have permission to publish “my-lib”. Are you logged in as the correct user?

npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/my-lib - You do not have permission to publish “my-lib”. Are you logged in as the correct user?
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\I042416\AppData\Roaming\npm-cache_logs\2021-10-17T02_44_36_666Z-debug.log

這個錯誤的原因是,我在 package.json 裏使用的庫名稱爲 my-lib, 這是我從 npm 教程裏照抄的,已經有人將同名的庫上傳到 npm 倉庫去了,因此我沒有權限上傳一個同名的庫。

將 package.json 裏的 name 字段修改即可:

之後就能夠成功上傳庫了:

更多Jerry的原創文章,盡在:“汪子熙”:

本文同步分享在 博客“汪子熙”(CSDN)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章