Skip to content

Electron Main Process Setup and Fix Guide

DodaTech Updated 2026-06-26 3 min read

What You Will Learn

In this guide you will learn how to configure Electron Main Process in Electron. A misconfigured electron main process causes crashes and silent failures. DodaTech uses proper Electron Main Process patterns in Doda Browser for stability across platforms.

Why it matters: Getting this wrong leads to cryptic errors, wasted debugging time, and unreliable application behavior.

Real-world use: Doda Browser manages dozens of windows simultaneously using Electron's multi-process architecture. Each tab runs in its own renderer process, and the main process coordinates window lifecycle, bookmark management, and download operations through a structured IPC layer. This architecture serves millions of daily active users across all major desktop platforms.

When you encounter issues, the debugging process is always the same: isolate the component that fails, check the input data format, verify the API call matches documentation, and confirm the output matches expectations. These steps apply regardless of the framework or language.

The Wrong Way

The main process entry point is called before Electron finishes initialization. Windows are created without lifecycle handling.

const { app, BrowserWindow } = require('electron');
let win = new BrowserWindow(); // Called before app is ready
win.loadFile('index.html');

The Right Way

const { app, BrowserWindow } = require('electron');
let win;
function createWindow() {
  win = new BrowserWindow({ width: 800, height: 600 });
  win.loadFile('index.html');
}
app.whenReady().then(() => {
  createWindow();
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

Expected output:

App window opens after ready. macOS stays alive when windows close. Other platforms quit.

Common Mistakes with Electron Main Process

  1. Creating windows before app ready: Calling new BrowserWindow() outside app.whenReady() is the most frequent mistake. The window appears blank because Electron's event loop is not initialized yet.

  2. Forgetting to register IPC handlers: If ipcMain.handle() is not registered before the renderer calls invoke(), the call times out silently. Always register handlers in the main process startup sequence.

  3. Synchronous operations in main process: Blocking the main process with synchronous file I/O or heavy computation freezes all windows. Use async APIs or worker threads instead.

  4. Not handling macOS lifecycle: On macOS, the app should stay active when all windows close. Missing the activate event handler causes the app to not respond when the dock icon is clicked.

Prevention

  • Always wrap window creation in app.whenReady()

  • Handle window-all-closed on non-macOS

  • Handle activate on macOS for dock click

  • Store window reference to prevent GC

  • Debug with console logs at each step to isolate the issue

  • Write unit tests for each component to catch regressions

  • Keep dependencies updated for bug fixes and improvements

  • Review official docs when upgrading to a new major version

  • Use version control and document your configuration changes

Next Steps

Now that you have fixed this issue, apply the same debugging approach to other Electron components. Each process boundary in Electron (main, renderer, preload) has specific rules about API access. Understanding these boundaries prevents entire categories of bugs. Practice by adding a new IPC channel for a feature you are building.

Common Mistakes with main process

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world ELECTRON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why is my window blank?

Created before ready. Move inside app.whenReady().

app.on('ready') vs app.whenReady()?

whenReady() returns a Promise and is the modern API.

Should I quit on macOS?

No -- standard is to stay alive when all windows close.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro