Electron is a powerful open-source framework that combines the functionality of Node.js and Chromium to create cross-platform desktop applications using web technologies. Electron allows you to leverage your Node.js knowledge to build full-fledged desktop apps for Windows, macOS, and Linux.
Electron was initially developed by GitHub to enable web developers to use familiar web technologies (HTML, CSS, and JavaScript) to build desktop applications. It merges the functionality of Node.js for backend operations and Chromium for frontend rendering. This framework is used by popular apps like Visual Studio Code, Slack, and Discord, demonstrating its robust capability.
mkdir electron-app
cd electron-app
npm init -y
npm install electron --save-dev
After this setup, you should see Electron listed in your package.json
dependencies.
Electron applications primarily involve three core components: the Main Process, Renderer Process, and Preload Scripts.
The main process controls the application lifecycle and manages application-level events. You define the main process in a JavaScript file (usually main.js
) to create windows and handle events.
The renderer process is where the user interface runs. Each window in an Electron app has its renderer process, which acts as a regular web environment, using HTML, CSS, and JavaScript.
Preload scripts run before any other scripts in the renderer process and are used to securely expose specific Node.js APIs to the renderer process.
Let’s create a simple “Hello World” Electron application.
main.js
In the project root, create a file named main.js
and add the following code:
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
}
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
app
and BrowserWindow
modules.createWindow
function initializes a new window and loads index.html
.index.html
Next, add index.html
in the project root:
Hello World
Hello, Electron!
In package.json
, add a start script:
"scripts": {
"start": "electron ."
}
npm start
Output: A window should open with the message “Hello, Electron!” displayed.
Electron uses the ipcMain
and ipcRenderer
modules to handle communication between the main and renderer processes.
// In main.js
const { ipcMain } = require('electron');
ipcMain.on('synchronous-message', (event, arg) => {
console.log(arg); // prints "ping"
event.returnValue = 'pong';
});
Electron applications rely on HTML, CSS, and JavaScript, allowing you to use front-end frameworks like React, Angular, or Vue.js.
index.html
:
This adds a button that sends a message to the main process and receives a response.
Output: When clicking “Click Me,” the paragraph text updates with “pong.”
Electron’s unique setup allows you to use Node.js modules directly in your application.
// In renderer process
const fs = require('fs');
fs.writeFile('test.txt', 'Hello from Electron!', (err) => {
if (err) throw err;
console.log('File has been saved!');
});
This will save a file called test.txt
in the project directory.
After developing your app, use electron-packager
or electron-builder
to package and distribute it.
electron-packager
// In renderer process
const fs = require('fs');
fs.writeFile('test.txt', 'Hello from Electron!', (err) => {
if (err) throw err;
console.log('File has been saved!');
});
npm install electron-packager –save-dev
// In renderer process
const fs = require('fs');
fs.writeFile('test.txt', 'Hello from Electron!', (err) => {
if (err) throw err;
console.log('File has been saved!');
});
Electron can work with native Node.js modules, which can provide extra functionality, though they may need to be rebuilt for Electron compatibility.
Electron apps need specific security practices, such as disabling nodeIntegration
and using secure IPC channels.
Electron apps can be optimized by managing memory efficiently, using lazy loading, and optimizing rendering.
Electron can be debugged using Chromium’s Developer Tools. Testing frameworks like Mocha and Spectron are commonly used for automated testing of Electron applications.
Electron and Node.js empower developers to build desktop applications using web technologies, offering a flexible and accessible approach to cross-platform app development. This chapter covered the fundamentals of setting up an Electron project, creating windows, communicating between processes, building a UI, and distributing the app. With Electron, developers can leverage their web development skills to build robust, professional-grade desktop applications. Happy Coding!❤️