Electron Introduction
Electron is an open-source framework that wraps Chromium (browser engine) + Node.js (server/system capabilities) into a desktop application shell, letting you use HTML/CSS/JS to write cross-platform desktop programs (Windows, macOS, Linux).
Key points:
- The front end handles the UI (Renderer process: like a browser tab).
- The main process (Main) manages the app lifecycle, windows, and native API calls.
- The two communicate through IPC (inter-process communication) to interact.
- Finally it can be packaged into
.exe,.app,.deband other installer files.
Electron is developed and maintained by GitHub and was originally created to build the Atom editor.
In short:Electron = Chromium + Node.js + Native APIs
- Chromium: provides a powerful rendering engine responsible for displaying the UI.
- Node.js: provides backend capabilities with access to the file system, network, etc.
- Native APIs: provides OS-level features such as menus, notifications, trays, etc.

Why choose Electron
Electron lets web developers build desktop apps without learning a new language, greatly lowering the barrier to desktop development.
- Quick to get started: Web developers can reuse a large number of front-end skills/components/ecosystems (React/Vue/Angular/TS/CSS).
- Cross-platform: Write once, build for three platforms (though system differences require adjustment).
- Powerful native capabilities: Directly access the file system, processes, and system APIs without writing native language bindings (Node.js is enough for most scenarios).
- Mature ecosystem: Plenty of ready-made libraries, templates, and packaging tools (Electron Builder / Forge, etc.).
- Suitable for tool-type applications: editors, communication tools, productivity software, debugging tools, etc.
Electron's design philosophy is "Write Once, Run Everywhere".
Developers only need to write one codebase to generate applications for the three platforms Windows, macOS, and Linux.
Core advantages:
- Develop desktop apps with a familiar web technology stack.
- No need to learn platform-specific languages (such as C++, Objective-C, Swift).
- A unified development experience and codebase.
- Fast iteration and deployment.
Application scenarios
Electron is suitable for developing the following types of desktop apps:
- Development tools / editors: code editors, terminals, debuggers (such as VS Code, Postman).
- Instant messaging / social: chat clients (such as Discord, Slack).
- Cross-platform desktop clients: when you need to quickly turn a web product into a desktop version.
- Prototypes and internal tools: internal company operations tools, data dashboards, admin consoles.
- Apps with high UI/interaction demands but not extremely performance-sensitive.
Unsuitable scenarios: projects extremely sensitive to CPU/GPU/memory, requiring the ultimate native experience, or mobile-first (such as large-scale graphics rendering, ultra-low-latency games).
What Electron can do
Electron provides rich desktop capabilities; the following are its main supported features:
- Open windows, menus, system tray, notifications (system notifications).
- Read/write files and operate local databases (SQLite, LevelDB, etc.).
- Invoke local commands/binaries and start child processes.
- Use native dialogs (open/save files), clipboard, drag-and-drop, global shortcuts.
- Implement auto-update, hot reload, and packaging/distribution.
- Load local or remote web pages with support for modern web technologies (WebGL, WebRTC, etc.).
- Integrate native modules (C++/Node Native Addons) to access hardware or system features.
Core concepts (technical points)
Understanding the following core concepts is key to mastering Electron development:
- Main process: The entry point of Electron, responsible for creating BrowserWindow, managing the app lifecycle and native APIs. Runs as a single instance (Node available).
- Renderer process: Each BrowserWindow corresponds to one renderer, which runs web pages (Chromium) and does not directly access Node (limited APIs can be exposed via preload).
- Preload scripts & contextIsolation: It is recommended to use preload to inject limited APIs into the renderer and enable contextIsolation for better security.
- IPC (ipcMain / ipcRenderer / contextBridge): The communication mechanism between main and renderer processes.
- Packaging tools: Electron Builder / Electron Forge / electron-packager, etc., used to generate installers and auto-updates.
- Auto-updater: Implements automatic app upgrades (requires server/publish configuration).
Security considerations
Electron has both browser and Node.js capabilities, so security configuration is critical.
Never directly enable in the renderer
nodeIntegration: true, disabled by default to prevent XSS from executing arbitrary system commands.
- Use contextIsolation + preload + contextBridge Expose minimal, controlled APIs.
- Strictly validate / sandbox external content (remote URLs).
- Strictly validate inputs such as file paths and shell commands to avoid command injection.
- Use CSP, disable eval, and avoid untrusted third-party scripts where possible.
Advantages and limitations of Electron
This section comprehensively analyzes Electron from both the advantages and limitations dimensions to help you make a sound technology choice.
Main advantages
The following are Electron's core competitive advantages over other desktop development solutions:
1. Convenience of cross-platform development
With Electron, developers can use the same codebase to generate apps for Windows, macOS, and Linux:
Example
// File path: main.js
// The same code can run on three platforms
const
{
app
,
BrowserWindow
}
=
require
(
'electron'
)
;
// Function to create the main window
function
createWindow
(
)
{
const
win
=
new
BrowserWindow
(
{
width
:
800
,
// Window width (pixels)
height
:
600
// Window height (pixels)
}
)
;
// Load a local HTML file
win.
loadFile
(
'index.html'
)
;
}
// Create the window after app is ready
app.
whenReady
(
)
.
then
(
createWindow
)
;
Costs saved:
- No need to maintain three separate codebases.
- The team does not need to master multiple platform development technologies.
- A unified testing and deployment process.
- Faster feature iteration.
2. Advantages of the web technology stack
- Rich ecosystem: Millions of packages are available on npm.
- Mature frameworks: You can use modern front-end frameworks such as React, Vue, Angular.
- High development efficiency: Hot reload, DevTools, rich debugging tools.
- Abundant talent pool: A huge number of web developers makes hiring easy.
- Rapid prototyping: Reuse existing web components and libraries.
3. Powerful feature support
- Full Node.js capabilities (file system, network, processes, etc.).
- Native desktop features (menus, notifications, system tray, etc.).
- Automatic update mechanism.
- Hardware access (camera, USB devices, etc.).
- Offline capability.
4. Active community and ecosystem
- Comprehensive official documentation.
- Many open-source projects for reference.
- Abundant third-party tools and libraries.
- Active community support.
Main limitations
Understanding Electron's limitations helps judge whether it fits your project:
1. Performance overhead
Electron apps need to run both Chromium and Node.js, which brings extra performance overhead:
- Memory Usage: A blank Electron app typically uses 50-100MB of memory.
- CPU usage: The rendering engine and JavaScript execution are less efficient than native apps.
- Startup time: Startup takes longer compared to native apps.
Native apps vs Electron apps:
| Comparison Dimensions | Native app | Electron app |
|---|---|---|
| Memory Usage | 10-30MB | 80-200MB |
| Startup time | < 1 second | 2-5 seconds |
2. Large installer size
Every Electron app must package the full Chromium and Node.js runtimes:
- Windows: ~50-70MB (uncompressed).
- macOS: ~60-80MB (uncompressed).
- Linux: ~55-75MB (uncompressed).
This means even a simple app's installer will not be smaller than 50MB.
3. Security considerations
Since Electron has both browser and Node.js capabilities, improper configuration may pose security risks:
- XSS attacks may lead to system-level privileges being exploited.
- Context isolation and sandbox need careful configuration.
- Security issues of third-party dependencies.
4. Native experience differences
Although native UIs can be mimicked, differences remain in some aspects:
- System native controls cannot be perfectly replicated.
- Some system-level features are hard to implement.
- Platform-specific interaction conventions need extra handling.
Trade-off recommendations
The following table helps you judge whether Electron fits your project requirements:
| Factor | Weight | Description |
|---|---|---|
| Development efficiency | High | If the team has web experience, Electron is the most efficient |
| Cross-platform needs | High | When multi-platform support is needed, Electron is the preferred choice |
| Performance requirements | Medium | Acceptable for general apps except extreme-performance scenarios |
| Sensitivity to package size | Medium | 50-100MB is acceptable in modern network environments |
| Security requirements | High | Security best practices must be strictly followed |
Comparison with other similar tools
Electron is not the only choice; the following is a comparison of mainstream desktop development solutions:
If you are a front-end/full-stack developer who needs toquickly deliver web products or tools to the desktop and directly use system capabilities, Electron is a highly productive, low-barrier choice.
But if you have strict requirements on size, memory, or the ultimate native experience, consider Tauri, Flutter, or native frameworks as alternatives.
-
Tauri
- Core: Rust + system-native WebView (WebView2 / WebKit).
- Pros: small size, low memory usage, good security, suitable for lightweight and performance-focused apps.
- Suitable for: desktop apps wanting web technology but extremely small size or performance sensitivity.
- Comparison: If you want to quickly reuse the Node.js ecosystem, Electron is more convenient; if you pursue small size, choose Tauri.
-
NW.js
- Similar origins to Electron (also Chromium + Node), but with a different API style.
- The Electron community and tools are more active.
-
Neutralino
- Extremely lightweight, uses the system WebView, with fewer features than Electron.
- Better suited for simple tools or small desktop apps.
-
Flutter Desktop
- Uses Dart/Flutter rendering, with good native performance and high UI consistency.
- Suitable for projects needing unified cross-platform UI/performance, but requires relearning a UI framework (not web technology).
-
React Native for Windows/Mac / .NET MAUI / Qt
- A more native desktop development route, suitable for projects with high requirements on system integration, performance, and native experience.
Selection advice:
- Want to quickly turn a web product into a desktop app, or the team is mainly web front-end -> Electron.
- Want small size / lower resource usage -> Tauri/Neutralino.
- Pursue native UI & high performance -> Flutter/Native/Qt.
Common performance and size questions
The following are practical tips for optimizing Electron app performance and size:
- Enable during packaging asar to merge resources (note native module compatibility).
- Use lazy loading and reduce initial rendering resources, minimizing heavy resources loaded by the main window.
- Avoid running many CPU-intensive tasks in the renderer; use instead child processes / worker / native module.
- Use single instance, and enable or disable hardware acceleration as appropriate.
- Optimize dependencies: reduce unnecessary npm packages and avoid introducing huge front-end libraries (or use on-demand loading).
Quick start example
Quickly understand Electron's basic structure through a minimal project.
Project file structure:
my-electron-app/
├─ package.json # 项目配置与依赖
├─ main.js # 主进程入口
├─ preload.js # 预加载脚本(安全暴露 API)
└─ index.html # 渲染进程界面
package.json(minimal configuration):
Example
{
"name": "my-electron-app",
"version": "0.1.0",
"main": "main.js", // Specify the main process entry file
"scripts": {
"start": "electron ." // npm start launches the app
},
"devDependencies": {
"electron": "latest" // Electron as a dev dependency
}
}
main.js(main process):
Example
// File path: main.js
const
{
app
,
BrowserWindow
}
=
require
(
'electron'
)
;
const
path
=
require
(
'path'
)
;
// Create the main window
function
createWindow
(
)
{
const
win
=
new
BrowserWindow
(
{
width
:
900
,
height
:
600
,
webPreferences
:
{
preload
:
path.
join
(
__dirname
,
'preload.js'
)
,
// Preload script path
contextIsolation
:
true
,
// Enable context isolation (recommended)
nodeIntegration
:
false
// Disable Node integration for better security
}
}
)
;
win.
loadFile
(
'index.html'
)
;
}
// Create the window after app is ready
app.
whenReady
(
)
.
then
(
createWindow
)
;
// macOS: do not quit the app when all windows are closed (follows macOS convention)
app.
on
(
'window-all-closed'
,
(
)
=>
{
if
(
process.
platform
!==
'darwin'
)
app.
quit
(
)
;
}
)
;
preload.js(controlled API exposure):
Example
// File path: preload.js
const
{
contextBridge
,
ipcRenderer
}
=
require
(
'electron'
)
;
// Expose safe APIs to the renderer via contextBridge
// The renderer can access them via window.electronAPI
contextBridge.
exposeInMainWorld
(
'electronAPI'
,
{
// Send messages to the main process (only whitelisted channels allowed)
send
:
(
channel
,
data
)
=>
{
const
validChannels
=
[
'ping'
]
;
// Whitelist: allowed channels
if
(
validChannels.
includes
(
channel
)
)
{
ipcRenderer.
send
(
channel
,
data
)
;
}
}
,
// Receive messages from the main process (only whitelisted channels allowed)
on
:
(
channel
,
func
)
=>
{
const
validChannels
=
[
'pong'
]
;
// Whitelist: allowed channels
if
(
validChannels.
includes
(
channel
)
)
{
// Remove the event parameter, passing only business data to the callback
ipcRenderer.
on
(
channel
,
(
event
,
...
args
)
=>
func
(
...
args
)
)
;
}
}
}
)
;
index.html(renderer process UI):
Example
Hello Electron
Listen for IPC in the main process (add to main.js):
Example
// File path: main.js (additional content)
const
{
ipcMain
}
=
require
(
'electron'
)
;
// Listen for ping messages from the renderer and reply with pong
ipcMain.
on
(
'ping'
,
(
event
,
data
)
=>
{
console.
log
(
'Received ping, data:'
,
data
)
;
event.
sender
.
send
(
'pong'
,
{
reply
:
'ok'
,
received
:
data
}
)
;
}
)
;
Run:
# Install dependencies
npm install
# Start application
npm start
Packaging and distribution
After development, you need to use packaging tools to distribute the app to users.
Comparison of common packaging tools:
| Tool | Function | Applicable scenarios |
|---|---|---|
| Electron Builder | Powerful, supports auto-update and multi-platform installers | Production projects needing a complete distribution solution |
| Electron Forge | Developer-friendly, integrates templates and build workflows | Fast startup for new projects |
| electron-packager | Only packages, no installer | Simple packaging needs |
Common packaging steps:
- Configure
package.jsonthe build field (or each tool's config). - Use CI/CD to build for different platforms (Windows must be packaged in a Windows environment or use cross-compile services).
- Deploy an update server or use GitHub Releases + electron-updater.
Practical advice and best-practice checklist
The following advice comes from real project experience and helps you avoid common pitfalls:
- At the start: using TypeScript + a packaging scaffold (Forge/Builder) saves later maintenance costs.
- Security: enforce
contextIsolation: true, minimizing the APIs exposed to the renderer. - Architecture: put business logic in the renderer (UI) or backend services; wrap system/privileged operations in the main process.
- Upgrades: use electron-updater for auto-updates, but pay attention to signing and release strategy (macOS requires signing, Windows also needs code signing).
- Testing: test on real machines across multiple platforms (especially file permissions, paths, environment variables).
- Resources: regularly check Chromium and Electron versions and watch security patches (Chromium vulnerabilities affect the app).
When not to use Electron
Electron is not recommended in the following scenarios:
- You need extremely low memory usage or a very small installer (e.g., embedded devices or minimal tools).
- You need the ultimate native UI and native performance (e.g., AAA games or complex CAD).
- The team is unfamiliar with web technology but familiar with native development, so a native framework may be more suitable.
Recommended learning path and resources
Following this path step by step lets you systematically master Electron development:
- Learn basic Node.js and front-end (HTML/CSS/JS) / frameworks (React/Vue).
- Read the official Electron documentation (Main/Renderer/Preload/IPC/security guide).
- Build a minimal example (the one above) and experiment with IPC, file operations, window control.
- Learn and use packaging tools (Electron Builder / Forge).
- Read tutorials on security best practices, auto-updates, and code signing.
- Study architecture by referencing mature open-source Electron projects (e.g., VS Code source snippets).