Electron Window Creation and Application Lifecycle
🎯 Introduction
You cannot create a window at any point in your code. Electron must finish initializing first, and the app must handle windows and processes according to platform conventions when quitting. This article covers these common lifecycle concerns without introducing menus or application-specific features.
By the end of this article, you will be able to:
- Create a
BrowserWindowat the right time. - Handle reactivation on macOS.
- Allow only one application instance to run.
- Keep and clear window references.
🧱 When to create a window
Create windows after app.whenReady() resolves. The main process can put the creation logic in a function and reuse it on initial startup and when the app is reactivated on macOS.
const { app, BrowserWindow } = require('electron/main');
const path = require('node:path');
let mainWindow;
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 960,
height: 640,
});
mainWindow.loadFile(path.join(__dirname, '../dist/index.html'));
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(() => {
createMainWindow();
});
Keeping a window reference lets the main process check whether the window exists, focus it, or send it messages later. Clear the reference when the window closes to avoid using an object that is no longer valid.
🪟 Handle activation and quitting
Common lifecycle hooks include:
| Event or method | Common use |
|---|---|
whenReady | Create a window after Electron finishes initializing |
activate | Restore a window when a macOS user activates the app again |
window-all-closed | Decide whether to quit after all windows close |
app.whenReady().then(() => {
createMainWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
On Windows and Linux, apps usually quit when all windows close. On macOS, the application process commonly keeps running so users can reopen a window from the Dock. Electron application events and a platform check let you implement this behavior.
🔒 Allow only one application instance
Desktop apps often need to prevent users from launching duplicate application processes. With requestSingleInstanceLock(), a newly launched instance can quit and notify the running instance to bring its window forward.
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', () => {
if (!mainWindow) {
return;
}
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
});
}
Place this code before window creation. It enforces a single application instance, but does not automatically handle additional data passed through deep links or file associations.
🧾 Summary
- Create
BrowserWindowinstances afterapp.whenReady()resolves. - Wrap window creation in a function so you can reuse it on startup and reactivation.
- Clear references when a window closes.
- On macOS, apps commonly recreate windows in response to
activate. requestSingleInstanceLockcan prevent duplicate application instances.
❓ Questions and answers
Q1: Why not create a window as soon as the file loads?
Electron has not finished preparing the application environment, so window creation may fail or behave inconsistently. Wait for whenReady.
Q2: Why check whether mainWindow is empty?
The reference is cleared when the window closes. Before sending messages or focusing it, check that the window still exists.
Q3: Does a single-instance lock prevent multiple windows?
No. The lock limits the number of application instances. One application process can still manage multiple windows.
🧪 Practice
Change the window width in createMainWindow to 1100, and complete the window display logic in the second-instance handler:
if (mainWindow) {
// Write your code here
}
🎉 Your app now has a reliable window lifecycle. Next, you will let Vue pages request main process capabilities through a preload script and IPC.
