Electron Getting Started Example
In the previous chapter we learned how to install Electron.
In this chapter we will step by step introduce how to create a simple Electron example.
1. Create the project directory
Open the terminal and run in order:
mkdir my-electron-app
cd my-electron-app
npm init -y
npm install electron --save-dev
The generated structure is as follows:
my-electron-demo/
├─ package.json
├─ main.js
└─ index.html
2. Configure package.json
Open package.json and modify it to the following content:
{
"name": "my-electron-app",
"version": "1.0.0",
"description": "RUNOOB Electron 测试",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "latest"
}
}
Explanation:
| Field | Purpose |
|---|---|
"main" |
Specifies the main process file to execute when Electron starts |
"scripts.start" |
define npm start The execution content of the command |
"electron ." |
Means letting Electron run the app in the current directory |
"devDependencies" |
A development dependency, not packaged into the final app |
3. Write the main process file main.js
This is Electron's core control center, responsible for creating app windows and loading pages.
Create the file main.js and enter the following complete code (with detailed comments):
Example
// main.js -- Electron main process file
// Import app (controls the app lifecycle) and BrowserWindow (creates windows) from the electron module
const
{
app
,
BrowserWindow
}
=
require
(
'electron'
)
;
// Node.js built-in module for joining file paths
const
path
=
require
(
'path'
)
;
// Create a function to generate the main window
function
createWindow
(
)
{
// Create a new window instance
const
win
=
new
BrowserWindow
(
{
width
:
800
,
// Window width (pixels)
height
:
600
,
// Window height (pixels)
resizable
:
true
,
// Whether to allow the user to drag to resize the window (default true)
title
:
"RUNOOB Electron Test"
,
// Window title, displayed in the title bar
webPreferences
:
{
// Web page runtime related configuration
contextIsolation
:
true
,
// Enable context isolation (security recommendation)
nodeIntegration
:
false
,
// Disable using Node.js directly in web pages
}
}
)
;
// Load the local index.html file into the window
// path.join(__dirname, 'index.html') returns the index.html path in the current directory
win.
loadFile
(
path.
join
(
__dirname
,
'index.html'
)
)
;
// Open DevTools (optional, for convenient debugging)
// win.webContents.openDevTools();
}
// When Electron finishes initialization, the app's ready event is triggered
// app.whenReady() returns a Promise; call createWindow() after it resolves
app.
whenReady
(
)
.
then
(
createWindow
)
;
// Triggered when all windows are closed (only effective outside macOS)
app.
on
(
'window-all-closed'
,
(
)
=>
{
// process.platform returns the operating system platform
// The macOS platform value is 'darwin'; on macOS the program generally stays resident
if
(
process.
platform
!==
'darwin'
)
{
app.
quit
(
)
;
// Quit the app
}
}
)
;
// macOS special logic: recreate the window when the Dock icon is clicked
app.
on
(
'activate'
,
(
)
=>
{
// If no windows are currently open, create a new one
if
(
BrowserWindow.
getAllWindows
(
)
.
length
===
0
)
{
createWindow
(
)
;
}
}
)
;
Explanation:
| Concept | Description |
|---|---|
| Main process | The script that runs first after Electron starts, responsible for window creation, menu control, system APIs, etc. |
| BrowserWindow | Used to create a browser window that loads web content (an HTML file or URL). |
| app.whenReady() | Ensure the window is created only after Electron finishes initialization, otherwise an error may occur. |
| win.loadFile() | Load a local HTML file. You can also use win.loadURL('https://example.com') to load a website. |
| contextIsolation | Isolate web page scripts from the Node environment to improve security (officially recommended). |
| nodeIntegration | If set to true, Node.js can be used directly in the page (but with security risks). |
| window-all-closed | Quit the app when all windows are closed (effective on Windows/Linux). |
4. Write the renderer page index.html
Create the index.html file and enter the following content:
Example
Runoob Tutorial(runoob.com)
Hello, Electron!
RUNOOB Electron Test. This is the first Electron desktop app you created
🎉
5. Run the project
Return to the terminal and run:
npm start
After running, you will see a desktop window displaying:
Hello, Electron!
RUNOOB Electron 测试。这是你创建的第一个 Electron 桌面应用 🎉
