Electron 教程

Electron 是什么、它如何把网页变成桌面应用,以及学习路线。

Beginner
Key PointsElectron 把 Chromium 和 Node.js 打包进一个可执行文件:界面用 HTML/CSS/JS 写,系统能力通过 Node 调用。

Electron 是什么

Electron builds desktop apps with web technology. It bundles Chromium (rendering) with Node.js (files, processes, system APIs), so one project both draws a UI and reads local files, shows native notifications, or starts at login.

它由什么组成

PartRole
Chromium渲染 HTML/CSS,执行页面 JS
Node.js文件、网络、进程、npm 生态
Native APIs窗口、菜单、托盘、剪贴板、通知
Packager把项目打成 exe / dmg / AppImage

谁在用它

VS Code, Slack, Discord, Figma desktop, Obsidian and Notion are all Electron apps. What they share: complex UI, cross-platform needs, and a team already fluent in web tech.

最小应用长什么样

Text
my-app/
├── package.json
├── main.js          ← 主进程:创建窗口、管生命周期
└── index.html       ← 渲染进程:界面
JavaScript
// main.js
const { app, BrowserWindow } = require('electron');

function createWindow() {
  const win = new BrowserWindow({ width: 900, height: 600 });
  win.loadFile('index.html');
}

app.whenReady().then(createWindow);
JSON
{
  "name": "my-app",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": { "start": "electron ." }
}

The main field points at the main process entry. electron . runs main.js first, which creates the window and loads the page — the page itself has no idea it is inside Electron.

An Electron app starts at a hundred-plus MB because it ships a whole Chromium. That is the price of using web tech; for a tiny utility, native or Tauri fits better.

学习路线

  1. 架构 —— 主进程、渲染进程、IPC 的分工
  2. 窗口 —— 创建、尺寸、无边框、多窗口
  3. 系统能力 —— 文件、菜单、托盘、通知、剪贴板
  4. 安全 —— contextIsolation、预加载脚本、CSP
  5. 工程化 —— 打包、签名、自动更新、发布

Friends & Partners