通過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 證書驗證。
設置命令如下:
- 首先通過
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'
}
- 通過上面可以發現
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".
- 在通過
yarn config get strict-ssl
確認strict-ssl
的值已經更改。(此步也可跳過,只是確認一下)
D:\Code\Web\StudyOrTest\electronStudy> yarn config get strict-ssl
false
- 再次運行安裝命令即可順利安裝
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
- 參考資料