配圖源自
背景
此前,在項目中安裝依賴時,遇到了如下報錯:
yarn install v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
error An unexpected error occurred: "https://r2.cnpmjs.org/form-data/-/form-data-3.0.1.tgz: unable to verify the first certificate".
info If you think this is a bug, please open a bug report with the information provided in "/Users/frankie/Web/ifanr/yuegonghui/activity-collection/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
yarn-error.log 如下
Arguments:
/Users/frankie/Library/Application Support/fnm/node-versions/v16.15.0/installation/bin/node /usr/local/bin/yarn
PATH:
/Users/frankie/Library/Caches/fnm_multishells/57063_1669556334889/bin:/Users/frankie/.yarn/bin:/usr/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/frankie/Library/Caches/fnm_multishells/57063_1669556334889/bin:/Users/frankie/.yarn/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/bin
Yarn version:
1.22.19
Node version:
16.15.0
Platform:
darwin arm64
Trace:
Error: unable to verify the first certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1532:34)
at TLSSocket.emit (node:events:527:28)
at TLSSocket._finishInit (node:_tls_wrap:946:8)
at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:727:12)
npm manifest:
...
yarn manifest:
No manifest
Lockfile:
...
報錯信息為: to the first ,與證書有關。由于 yarn 或 npm 走的是 HTTPS 協議瀏覽器出現證書錯誤,它的安全通過數字證書來保障。數字證書由專門機構頒發,通常是付費的。自簽證書,就是自己扮演數字證書機構給自己頒發的證書。
由于自 2014 年 2 月 27 日起,npm 不再支持「自簽證書 Self- 」。 npm Blog
加上,也就是 所在域名的證書是不被信任的。這點通過 瀏覽器就能發現:
其中 npm 與證書相關的配置有兩項
解決方法方法一
在確定「安全」的情況下,可以臨時關閉 -ssl 選項:
$ yarn config set strict-ssl false
$ npm config set strict-ssl false
當 -ssl 設置為 false 時,npm 將不會對服務器的 SSL 證書進行校驗,并且即使證書是由不可信的認證機構頒發的也不會報錯。這可能會導致安全風險,因為你的網絡流量可能被劫持或篡改,而你并不會意識到這一點。因此,應該盡量避免使用 -ssl 設置為 false。
如果你確實需要使用 -ssl 設置為 false,例如你所連接的服務器使用的是自簽名的 SSL 證書瀏覽器出現證書錯誤,應該只在短時間內使用,并在操作完成后盡快將 -ssl 設置回 true。
方法二(推薦)
找出所有相關的 npm 包,并選擇可信的鏡像源后重裝。