Electron 安装

Node 环境准备、npm 安装 Electron、镜像加速、验证安装。

Beginner
Key PointsElectron 是一个 npm 包,装在项目里而不是全局;安装时会下载对应平台的预编译二进制。

前置条件

Bash
node -v
npm -v
Output
v22.22.2
10.9.7

Electron needs Node to run npm and the packaging tools, but the app runtime does not use the system Node — it ships its own. End users never install Node.

装到项目里

Bash
mkdir my-app && cd my-app
npm init -y
npm install --save-dev electron
Output
added 78 packages in 12s

Use --save-dev, not --save: Electron is needed only for development and packaging. Some packagers copy dependencies into the bundle, adding tens of MB.

国内镜像加速

Bash
# 临时
ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/ npm install --save-dev electron

# 永久(写进 ~/.npmrc)
electron_mirror=https://npmmirror.com/mirrors/electron/

npm install electron additionally fetches a tens-of-MB prebuilt binary from GitHub, which often times out in China. The mirror only affects that download, not the npm package itself.

验证

Bash
npx electron --version
Output
v33.0.0

npx uses the project-local copy instead of a global one. A global install tends to drift from the project version — not recommended.

常见安装问题

SymptomCause
Download failedGitHub 不可达,需要配镜像
EACCES / permission用了 sudo npm i -g,或目录权限不对
Version mismatch全局与项目各装了一份
Cannot find modulemain 字段指向的文件不存在
App won't start on Linux缺 libnss3、libgbm 等系统库
The "main" field must point at a real file. When it does not, Electron reports Unable to find Electron app without mentioning main — a long detour for a one-character typo.

Friends & Partners