Electron Core Concepts
Electron is essentially a multi-process desktop application framework that combines Chromium (rendering layer) and Node.js (system layer), implementing UI display, logic control, and secure communication through an independent process model.

Below is the core Electron architecture, showing the main process, renderer process, IPC communication, and the Preload relationship.
Main process (A):
- Runs Node.js, controls windows and system interaction.
- Uses BrowserWindow to create renderer processes.
** Renderer process (B):**
- Each window runs HTML/CSS/JS independently to display the interface.
** Preload script (B2):**
- Runs before the renderer process loads, connecting the main process and the web page layer.
- Safely exposes APIs through contextBridge.
IPC communication (dashed arrows):
- The main process and renderer process communicate bidirectionally via ipcMain and ipcRenderer.
Operating system (C):
- The main process can access system features such as files, tray, and notifications.

Main Process
The main process is the "command center" of an Electron app, running in the Node.js environment.
It is responsible for creating windows, managing the app lifecycle, calling system APIs, and communicating with the renderer process.
After the whole app starts, the system first runs the main process script (usually main.js).
Responsibilities of the main process
- Start and exit the application
- Create and destroy BrowserWindow (windows)
- Register menus, tray, global shortcuts
- Call operating system functions (file system, notifications, dialogs)
- Listen for events sent by the renderer process
Main process lifecycle
The main process lifecycle matches the application lifecycle, starting from the app module:
Example
const
{
app
,
BrowserWindow
}
=
require
(
'electron'
)
;
app.
whenReady
(
)
.
then
(
(
)
=>
{
const
win
=
new
BrowserWindow
(
{
width
:
800
,
height
:
600
}
)
;
win.
loadFile
(
'index.html'
)
;
}
)
;
app.
on
(
'window-all-closed'
,
(
)
=>
{
if
(
process.
platform
!==
'darwin'
)
app.
quit
(
)
;
}
)
;
app.whenReady(): triggered after Electron finishes initialization.window-all-closed: triggered when all windows are closed.app.quit(): quit the entire application.- Common special case on macOS: when the user closes all windows, the program still keeps running in the background.
app module details
app The module is used to control the entire application lifecycle:
app.on('ready'): triggered when Electron has finished initialization.app.quit(): quit the application.app.getPath('userData'): get system paths.app.setAppUserModelId(id): set the Windows taskbar ID.app.isPackaged: determine whether it is currently packaged.
BrowserWindow window management
The main process passes BrowserWindow to create a visible window:
Example
const
win
=
new
BrowserWindow
(
{
width
:
1024
,
height
:
768
,
resizable
:
true
,
webPreferences
:
{
preload
:
'./preload.js'
,
nodeIntegration
:
false
}
}
)
;
width,height: window sizeresizable: whether it is resizable-
webPreferences: used to configure renderer process behavior preload: preload scriptnodeIntegration: whether to allow the renderer process to use Node.js
Native features such as menus, tray, and dialogs
Electron provides multiple modules to access system features:
- Menu / MenuItem: custom menu bar
- Tray: create a system tray icon
- Dialog: call the system file selector and warning boxes
- Notification: native notifications
-
shell: open external links or files, for example:
const { dialog } = require('electron');
dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] });
Renderer Process
The renderer process is responsible for displaying the interface, running in the Chromium environment.
Each BrowserWindow has an independent renderer process.
Characteristics of the renderer process
- Based on Chromium, runs HTML/CSS/JS
- Default Cannot directly access Node.js APIs(for security reasons)
- Can use web technology stacks: Vue, React, Svelte, etc.
- Each window corresponds to an independent thread and does not affect the others
Multiple windows and multiple renderer processes
Each Electron window (BrowserWindow) starts an independent renderer process:
Closing one window does not affect the others, similar to the browser's multi-tab model.
This makes the app safer and more reliable in crash recovery.
Loading and displaying web pages
The content of the renderer process is loaded by the main process:
win.loadFile('index.html'); // 加载本地页面
// 或者
win.loadURL('https://example.com'); // 加载远程网页
Restrictions in the renderer process
- For security reasons, you cannot directly use Node.js modules (such as
fs,path). - It is not recommended to perform heavy computation in the renderer process (it will freeze the UI).
- Interaction with the system requires IPC communication or APIs exposed by preload.
Inter-Process Communication (IPC)
One of Electron's core mechanisms is communication between the main process and the renderer process, called IPC (Inter-Process Communication).
ipcMain and ipcRenderer
ipcMain: used in the main process to receive messages.ipcRenderer: used in the renderer process to send messages.
One-way communication example:
Example
// main.js
const
{
ipcMain
}
=
require
(
'electron'
)
;
ipcMain.
on
(
'ping'
,
(
event
,
arg
)
=>
{
console.
log
(
arg
)
;
// outputs "hello from renderer"
}
)
;
// renderer.js
const
{
ipcRenderer
}
=
require
(
'electron'
)
;
ipcRenderer.
send
(
'ping'
,
'hello from renderer'
)
;
Two-way communication example (main process returns data):
ipcMain.on('getVersion', (event) => {
event.reply('getVersionResponse', app.getVersion());
});
Received on the renderer side:
ipcRenderer.on('getVersionResponse', (event, version) => {
console.log(version);
});
invoke/handle pattern (recommended approach)
Electron provides a simpler asynchronous request method:
Main process:
ipcMain.handle('get-app-path', () => app.getPath('userData'));
Renderer process:
const result = await ipcRenderer.invoke('get-app-path');
console.log(result);
This approach is similar to fetch requests, avoiding multiple layers of callbacks.
Best practices for message passing
- Use Explicit channel names(such as
"user:getData"). - Avoid passing complex objects (JSON recommended).
- Validate the source and type of data from the renderer process.
- As much as possible, use
invoke/handleto implement structured communication.
Preload Scripts
Preload scripts run before the renderer process is created, between the main process and the web page.
Their role is to safely "bridge" Node.js functionality to the renderer page.
The role and significance of preload
- Can access Node.js APIs (because it is in an isolated context)
- Can safely expose a small number of APIs to the renderer page
- Commonly used for file reading/writing, getting system paths, passing app information, etc.
// preload.js
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', {
getAppVersion: () => ipcRenderer.invoke('getVersion')
});
Used by the renderer process:
// index.html
contextBridge secure communication
contextBridge is a secure API provided by Electron, used to:
- Expose only the functions needed (whitelist mechanism)
- Prevent malicious web pages from accessing the Node.js environment
- in
contextIsolation: truemode still allows secure communication
Usage in sandbox mode
If the sandbox is enabled (sandbox: true), the renderer process cannot use Node.js at all.
At this point preload is the only bridge, responsible for:
- Receiving calls from the renderer side
- Forwarding to the main process via IPC
- Returning results to the web page side
Summary
| module | Purpose | Runtime environment |
|---|---|---|
| Main process (Main) | Controls the app lifecycle, windows, system interfaces | Node.js |
| Renderer process (Renderer) | Displays the UI, executes frontend logic | Chromium |
| IPC | Implements communication between the main and renderer processes | Two-way pipeline |
| Preload | Safely bridges Node and Web | Middle layer |
In one sentence:
The main process is like the "control room", the renderer process is like "staff at each window",
IPC is the "walkie-talkie", and Preload is the "filtered secure channel".