Loading...

yarn安装包时报 `unable to verify the first certificate` 错误

通过yarn包管理器安装 electron-webpack 时,报如下错误:

D:\Code\Web\StudyOrTest\electronStudy> yarn add webpack --dev
yarn add v1.22.10
[1/4] Resolving packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/webpack: unable to verify the first certificate".
info If you think this is a bug, please open a bug report with the information provided in "D:\\Code\\Web\\StudyOrTest\\electronStudy\\yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
  • 问题原因

开始以为是下载源的问题,但是切换到淘宝源后依然无法解决问题,还是报这个问题。并且自己通过npm包管理器安装时,也会报同样的错误。

后来在网上找了一圈,发现是【HTTPS 证书验证失败】导致的。

  • 解决方案

【将yarn配置中的 strict-ssl 设置为 flase 】, 在 info yarn config 信息中, 'strict-ssl' 为 true,表示需要验证 HTTPS 证书。我们可以将 'strict-ssl' 设置为 false,跳过 HTTPS 证书验证。

设置命令如下:

  1. 首先通过 yarn config list 查看yarn的配置清单(这一步只是确认,可以跳过):
D:\Code\Web\StudyOrTest\electronStudy> yarn config list
yarn config v1.22.10
info yarn config
{
  'version-tag-prefix': 'v',
  'version-git-tag': true,
  'version-commit-hooks': true,
  'version-git-sign': false,
  'version-git-message': 'v%s',
  'init-version': '1.0.0',
  'init-license': 'MIT',
  'bin-links': true,
  'ignore-scripts': false,
  'ignore-optional': false,
  'strict-ssl': true,
  'user-agent': 'yarn/1.22.10 npm/? node/v12.18.3 win32 x64',
  ELECTRON_MIRROR: 'https://cdn.npm.taobao.org/dist/electron/',
  lastUpdateCheck: 1605235165526,
  'sass-binary-site': 'http://npm.taobao.org/mirrors/node-sass'
}
info npm config
{
  tb_registry: 'https://registry.npm.taobao.org',
  registry: 'https://registry.npm.taobao.org/',
  disturl: 'https://npm.taobao.org/dist'
}
  1. 通过上面可以发现 strict-ssl 的值为 true ,通过如下命令将其改为 false 即可。
D:\Code\Web\StudyOrTest\electronStudy> yarn config set strict-ssl false
yarn config v1.22.10
success Set "strict-ssl" to "false".
  1. 在通过 yarn config get strict-ssl 确认 strict-ssl 的值已经更改。(此步也可跳过,只是确认一下)
D:\Code\Web\StudyOrTest\electronStudy> yarn config get strict-ssl
false
  1. 再次运行安装命令即可顺利安装
D:\Code\Web\StudyOrTest\electronStudy> yarn add electron-webpack --dev 
yarn add v1.22.10
[1/4] Resolving packages...
warning electron-webpack > webpack-dev-server > chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
warning electron-webpack > webpack-dev-server > chokidar > fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
warning electron-webpack > webpack-dev-server > chokidar > braces > snapdragon > source-map-resolve > resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
warning electron-webpack > webpack-dev-server > chokidar > braces > snapdragon > source-map-resolve > urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
[2/4] Fetching packages...
info fsevents@1.2.13: The platform "win32" is incompatible with this module.
info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning "electron-webpack > mini-css-extract-plugin@0.9.0" has incorrect peer dependency "webpack@^4.4.0".
warning "electron-webpack > webpack-cli@3.3.12" has incorrect peer dependency "webpack@4.x.x".
warning "electron-webpack > webpack-dev-server > webpack-dev-middleware@3.7.2" has incorrect peer dependency "webpack@^4.0.0".
warning " > electron-webpack@2.8.2" has incorrect peer dependency "webpack@^4.42.1".
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 429 new dependencies.
....后面是安装信息省略了
  • 问题延展

前面说到过自己通过npm包管理器安装时也遇到过类似的问题,报如下错误:

D:\Code\Web\StudyOrTest\electronStudy> npm install --save-dev webpack
npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR! errno UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR! request to https://registry.npm.taobao.org/webpack failed, reason: unable to verify the first certificate

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2020-11-14T02_16_58_410Z-debug.log

问题原因也是和上面一样的,都是【HTTPS 证书验证失败】。解决办法也是一样的,将npm包管理器的 strict-ssl 的值改为 false 即可。修改命令如下:

D:\Code\Web\StudyOrTest\electronStudy> npm config set strict-ssl false		// 修改
D:\Code\Web\StudyOrTest\electronStudy> npm config get strict-ssl      		// 确认值是否修改
false
  • 参考资料
  1. 执行 yarn install 抛出 unable to verify the first certificate 错误
posted @ 2020-11-14 11:32  Penn00  阅读(6913)  评论(0编辑  收藏  举报