---
url: /guide/desktop/maintenance/electron-faq.md
---
# Electron 常见问题 FAQ

## 1. path.txt 文件不存在或内容损坏

**错误信息 A - 文件不存在**：

```
Error: ENOENT: no such file or directory, open 'node_modules\electron\path.txt'
```

**错误信息 B - 路径含换行符**：

```
Error: spawn node_modules\electron\dist\electron.exe\r\n ENOENT
```

**原因**：

* npm postinstall 脚本（`node install.js`）下载 Electron 二进制文件失败
* 导致 `path.txt` 文件缺失或包含 Windows 换行符（`\r\n`）

**快速诊断**：

```powershell
cd node_modules\electron
Get-Content "path.txt" -Raw  # 查看文件内容
```

**快速修复**：

```powershell
cd node_modules\electron
# 方法 1：ASCII 编码写入（推荐）
node -e "require('fs').writeFileSync('path.txt', 'electron.exe', 'ascii')"

# 方法 2：验证二进制是否存在
if (!(Test-Path "dist\electron.exe")) {
    Write-Host "需要手动下载 Electron 二进制"
}
```

**根本解决**：确保 npm install 时能正确下载二进制

```powershell
$env:ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
npm install electron@版本号 --save-dev
```

**手动下载方案**（当网络无法连接时）：

1. 从 https://npmmirror.com/mirrors/electron/ 下载对应版本的 zip
2. 解压到 `node_modules/electron/dist/` 目录
3. 执行修复命令：

```powershell
Expand-Archive -Path "electron-v44.7.0-win32-x64.zip" -DestinationPath "node_modules\electron\dist" -Force
node -e "require('fs').writeFileSync('node_modules\\electron\\path.txt', 'electron.exe', 'ascii')"
```

***

## 2. Electron 二进制下载失败 (ETIMEDOUT / fetch failed)

**错误信息 A - 连接超时**：

```
RequestError: connect ETIMEDOUT 66.220.148.145:443
npm error command failed
npm error command node install.js
```

**错误信息 B - fetch 失败**：

```
TypeError: fetch failed
    at node:internal/deps/undici/undici:15845:13
    at async FetchDownloader.download (...)
Downloading Electron binary...
```

> **注意**：`npm run dev:electron` 的 vite 插件会 spawn `electron.exe`，当 `node_modules/electron/dist` 缺失时，下载会在 **dev 阶段**触发，报错不一定出现在 `npm install` 时。

**原因**：下载器最终回落到 GitHub 官方源（国内网络无法连接）。回落分两类触发路径，先分清再动手：

1. 镜像变量完全没配置；
2. 镜像变量已写入 User / Machine 级环境变量，但**当前进程没有继承**——IDE 或终端启动早于环境变量设置，`$env:ELECTRON_MIRROR` 为空，`@electron/get` 找不到镜像配置后回落到默认 GitHub。

**先诊断（10 秒）**：

```powershell
# 1) 系统级是否配置（本机应输出 https://npmmirror.com/mirrors/electron/）
[Environment]::GetEnvironmentVariable('ELECTRON_MIRROR','User')
[Environment]::GetEnvironmentVariable('ELECTRON_MIRROR','Machine')

# 2) 当前进程是否继承（为空 = 未继承，即原因 2）
$env:ELECTRON_MIRROR

# 3) 两个源连通性对比（GitHub 超时 + npmmirror 200 = 确认是网络限制）
Invoke-WebRequest -Uri "https://npmmirror.com/mirrors/electron/44.7.0/electron-v44.7.0-win32-x64.zip" -Method Head -TimeoutSec 25 -UseBasicParsing
```

**解决方案**：

### 方案 A：让进程拿到镜像变量（推荐，无需手动下载）

镜像**不再写进 `.npmrc`**（npm v11+ 会对非标准键告警，见第 3 节），统一依赖系统环境变量 `ELECTRON_MIRROR`（本机 User + Machine 两级均已设置）。

```powershell
# 已继承：直接重装即可
npm install

# 未继承：重启 IDE / 新开终端让子进程继承，或在当前会话临时设置后重装
$env:ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
npm install
```

`@electron/get` 读取镜像的顺序：`npm_config_electron_mirror` > `NPM_CONFIG_ELECTRON_MIRROR` > `npm_package_config_*` > `ELECTRON_MIRROR` > 默认 GitHub。

### 方案 B：手动下载并解压（完整流程）

当网络无法连接时，按以下步骤手动完成 Electron 二进制安装：

**步骤 1：确认需要的 Electron 版本**

```powershell
# 查看 package.json 中声明的版本
Get-Content "packages\electron-host\package.json" | Select-String "electron"
# 或查看 node_modules 中的版本
Get-Content "node_modules\electron\package.json" | Select-String '"version"'
```

**步骤 2：下载二进制压缩包**

访问镜像源：https://npmmirror.com/mirrors/electron/

* 找到对应的 `v版本号` 目录（如 `v44.7.0`）
* 下载 `electron-v版本号-win32-x64.zip` 文件

**步骤 3：解压到正确位置**

```powershell
# 切换到 electron 模块目录
cd node_modules\electron

# 解压（会解压到当前目录的 dist 子目录）
Expand-Archive -Path "D:\Downloads\electron-v44.7.0-win32-x64.zip" -DestinationPath "dist" -Force
```

**步骤 4：创建 path.txt 文件**

```powershell
node -e "require('fs').writeFileSync('path.txt', 'electron.exe', 'ascii')"
```

**步骤 5：验证安装**

```powershell
# 检查文件是否存在
Test-Path "dist\electron.exe"

# 检查 path.txt 内容
Get-Content "path.txt" -Raw

# 测试运行
.\dist\electron.exe --version
```

**成功后应看到**：

* `Test-Path` 返回 `True`
* `path.txt` 内容为 `electron.exe`（无换行符）
* `electron.exe --version` 显示版本号

**实测记录（2026-10，Electron 44.7.0）**：GitHub release 的 HEAD 请求直接超时；npmmirror 下载 158,293,649 bytes 用时 25 秒，SHA256 `eee30dc8…2665c` 与镜像 `SHASUMS256.txt` 一致。校验命令：

```powershell
(Get-FileHash "electron-v44.7.0-win32-x64.zip" -Algorithm SHA256).Hash.ToLower()
```

***

## 3. npm 警告 "Unknown project config"

**警告信息**：

```
npm warn Unknown project config "electron_mirror"
npm warn Unknown project config "disturl"
```

**说明**：这是提示性警告，npm 未来版本（v10+）将不再支持这些非标准配置项

**影响**：

* 开发环境仍可正常工作
* 建议迁移到标准环境变量方式

**替代方案**：

```powershell
# 使用环境变量代替 .npmrc 配置
$env:ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
$env:ELECTRON_CUSTOM_DIR="v44.7.0"  # 必须包含 'v' 前缀
```

***

## 4. Electron 下载 404 问题

**错误信息**：

```
GET https://npmmirror.com/mirrors/electron/v44.7.0/electron-v44.7.0-win32-x64.zip 404
```

**原因**：

* `ELECTRON_CUSTOM_DIR` 路径格式错误
* 缺少 `v` 前缀

**正确配置**：

```powershell
# 错误示例
$env:ELECTRON_CUSTOM_DIR="44.7.0"  # ❌ 缺少 v 前缀

# 正确示例
$env:ELECTRON_CUSTOM_DIR="v44.7.0"  # ✅ 包含 v 前缀
```

> 实测：npmmirror 对 `44.7.0/` 与 `v44.7.0/` 两种目录都返回 200；但 GitHub 官方只认 `v` 前缀，`ELECTRON_CUSTOM_DIR` 拼接时也默认带 `v`，因此统一写成 `v<版本号>`。

***

## 5. Windows PowerShell 命令兼容性

**问题**：PowerShell 不支持某些 Linux 命令

**常用替代**：
| Linux 命令 | PowerShell 命令 |
|-----------|----------------|
| `rm -rf` | `Remove-Item -Recurse -Force` |
| `head -n 10` | `Select-Object -First 10` |
| `&&` (并行) | `;` (串行) |
| `cat` | `Get-Content` |
| `ls` | `Get-ChildItem` |

***

## 6. pnpm workspace 警告

**警告信息**：

```
WARN  The "workspaces" field in package.json is not supported by pnpm.
Create a "pnpm-workspace.yaml" file instead.
```

**解决方案**：创建 `pnpm-workspace.yaml`

```yaml
packages:
  - 'packages/*'
  - 'packages/micro-apps/*'
```
