Electron Tutorial
Electron (formerly Atom Shell) is an open-source cross-platform desktop application development framework developed by GitHub.
Electron is an open-source framework specifically for building cross-platform desktop applications using Web technologies (HTML, CSS, and JavaScript).
Electron lets us write desktop apps just like writing web pages, such as Windows .exe, macOS .app, and Linux executables.
Electron = Chromium (browser engine) + Node.js (backend capability) + desktop app shell.
The basic knowledge needed to learn Electron?
Before learning Electron, mastering these is enough:
- Know a bit of JavaScript.
- Know how to use Node.js and npm to run scripts.
- Understand a bit of HTML and selectors(for example
#id,.class); - Can use the terminal to run commands.
Core Concepts
Imagine that when you normally visit a website with a browser, what you see is a page built with HTML, styled with CSS, and with interactive features implemented in JavaScript. Electron is like bundling a browser "up so it can run independently, no longer requiring the user to manually open a browser.
Technical composition:
- Chromium: provides the page rendering engine to ensure your app UI displays correctly
- Node.js: provides system-level APIs, letting you access low-level functions such as the file system and network
- Native APIs: allows calling native OS features such as menus and dialogs
Why choose Electron?
Development advantages
1. Unified tech stack
- Develop with familiar Web technologies
- Front-end developers can get started quickly
- Rich Web ecosystem resources are available
2. Cross-platform support
- Develop once, run on multiple platforms
- Supports Windows, macOS, Linux
- UI and features stay consistent across different systems
3. High development efficiency
- Hot reload feature for real-time preview of changes
- Complete debugging tools, can use Chrome DevTools
- Active community, problems solved quickly
Example
Suppose we want to make a notepad app.
In Electron, you only need to:
npm install electron
Create a main.js:
Example
const
{
app
,
BrowserWindow
}
=
require
(
'electron'
)
;
app.
whenReady
(
)
.
then
(
(
)
=>
{
const
win
=
new
BrowserWindow
(
{
width
:
800
,
height
:
600
}
)
;
win.
loadFile
(
'index.html'
)
;
// This is your web page UI
}
)
;
Then index.html is just an ordinary web page:
Example
My Desktop Notepad
Run:
npx electron .
This way our web page turns into a desktop app!
Reference Resources
- Electron official website
- Electron official documentation
- Electron Fiddle - A tool to experiment with and share Electron code snippets
- awesome-electron - A collection of Electron resources