Building Apps with Electron
Electron is an open-source framework that allows developers to build cross-platform desktop applications using HTML, CSS, and JavaScript. It combines the power of Chromium (for rendering) and Node.js (for backend logic) into a single runtime, enabling web developers to create native desktop apps without learning new languages.
1. What Electron Is
Electron was originally developed by GitHub and powers popular apps like Visual Studio Code, Slack, and Discord. It wraps your web app inside a native shell, giving access to system APIs such as file handling, notifications, and menus.
Core Components
- Main Process: Controls the app lifecycle and system-level operations.
- Renderer Process: Runs the web interface (HTML/CSS/JS) using Chromium.
- Preload Scripts: Securely bridge communication between main and renderer processes.
2. What You Can Build with Electron
- Point-of-sale (POS) systems and inventory managers.
- Offline desktop dashboards and data visualizers.
- Cross-platform editors, IDEs, and productivity tools.
- Hybrid SaaS clients that sync with cloud APIs.
3. Installing Electron
Windows Setup
# Install Node.js (LTS version recommended) https://nodejs.org/ # Create a new folder and initialize npm mkdir electron-app cd electron-app npm init -y # Install Electron globally or locally npm install electron --save-dev
Linux Setup
# Install Node.js and npm sudo apt update sudo apt install nodejs npm -y # Create project folder mkdir electron-app cd electron-app npm init -y # Install Electron npm install electron --save-dev
4. Minimal Configuration Files
package.json
{
"name": "electron-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^30.0.0"
}
}
main.js
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 1000,
height: 700,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
index.html
My Electron App
Hello from Electron!
This is a simple desktop app built with HTML, CSS, and JS.
5. Running and Building
# Run the app in development mode npm start # Package the app for distribution npm install --save-dev electron-packager # Example build command for Windows npx electron-packager . MyApp --platform=win32 --arch=x64 --out=dist # Example build command for Linux npx electron-packager . MyApp --platform=linux --arch=x64 --out=dist
6. Common Dependencies
| Dependency | Purpose |
|---|---|
| electron | Main framework runtime |
| electron-packager | Builds distributable executables |
| electron-builder | Advanced packaging and auto-update support |
| dotenv | Manages environment variables |
| sqlite3 / better-sqlite3 | Local database storage |
7. Final Notes
Electron makes it possible to turn any web app into a native desktop experience. With minimal configuration, you can build, test, and package apps for Windows, Linux, and macOS using the same codebase. For lightweight alternatives, frameworks like Tauri or NeutralinoJS offer smaller binaries, but Electron remains the most mature ecosystem for production-grade desktop apps.