我今天照着 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的原創文章,盡在:"汪子熙":