You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

全局安装Electron时遇unable to verify the first certificate错误求助

问题描述

尝试全局安装Electron时持续报错unable to verify the first certificate,具体细节如下:

执行命令

npm install electron -g

核心错误

unable to verify the first certificate

错误日志片段

npm WARN cleanup Failed to remove some directories [
npm WARN cleanup   [
npm WARN cleanup     'E:\\ProgramLang\\NVM\\v20.6.1\\node_modules\\electron\\node_modules\\@szmarczak',
npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'E:\ProgramLang\NVM\v20.6.1\node_modules\electron\node_modules\@szmarczak\http-timer'] {
...
npm ERR! RequestError: unable to verify the first certificate
...
npm ERR! A complete log of this run can be found in: C:\Users\Yu\AppData\Local\npm-cache\_logs\2023-09-16T11_15_26_872Z-debug-0.log

已尝试但无效的操作

  • 以管理员身份运行PowerShell
  • 设置环境变量NODE_TLS_REJECT_UNAUTHORIZED=0
  • 使用淘宝镜像并添加--strict-ssl=false参数:
    npm install electron -g --registry=https://registry.npm.taobao.org --strict-ssl=false
    
  • 切换至Yarn全局安装,仍报相同证书错误:
    PS C:\Users\Yu> yarn global add electron
    yarn global v1.22.19
    warning You don't appear to have an internet connection. Try the --offline flag to use the cache for registry queries.
    warning package.json: No license field
    [1/4] Resolving packages...
    [2/4] Fetching packages...
    [3/4] Linking dependencies...
    [4/4] Building fresh packages...
    error C:\Users\Yu\AppData\Local\Yarn\Data\global\node_modules\electron: Command failed.
    Exit code: 1
    Command: node install.js
    Arguments:
    Directory: C:\Users\Yu\AppData\Local\Yarn\Data\global\node_modules\electron
    Output:
    RequestError: unable to verify the first certificate
    ...
    info Visit https://yarnpkg.com/en/docs/cli/global for documentation about this command.
    PS C:\Users\Yu>
    

补充信息

全局安装Express无此问题,说明仅Electron安装受证书校验问题影响。


解决方案

1. 配置Electron专属镜像(关键步骤)

Electron安装时会单独下载二进制包,该流程不受npm registry配置影响,需单独设置Electron镜像:

  • 临时生效(当前终端):
    # 使用npm安装
    set ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
    npm install electron -g
    
    # 使用Yarn安装
    set ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
    yarn global add electron
    
  • 永久生效:
    执行命令添加配置到npm全局设置:
    npm config set ELECTRON_MIRROR https://npmmirror.com/mirrors/electron/
    
    或手动在用户目录的.npmrc文件中添加:ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/

2. 针对Electron安装脚本忽略证书校验

若镜像配置后仍有证书问题,需确保安装脚本进程读取到跳过校验的变量:

set NODE_TLS_REJECT_UNAUTHORIZED=0 && npm install electron -g

3. 清理残留文件后重试

之前的安装残留可能引发权限或缓存问题,先清理再安装:

# 清理npm缓存
npm cache clean --force

# 删除Electron残留目录(根据日志路径调整)
rmdir /s /q "E:\ProgramLang\NVM\v20.6.1\node_modules\electron"
rmdir /s /q "C:\Users\Yu\AppData\Local\Yarn\Data\global\node_modules\electron"

4. 指定Electron版本安装

若最新版本镜像同步异常,可尝试安装稳定LTS版本:

npm install electron@25.8.0 -g

内容的提问来源于stack exchange,提问作者isYuAh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:25:05