This guide explains how to integrate Next.js with Electron, bundle the app, and generate a Windows .exe file using electron-builder.
Ensure you have the following installed:
- Node.js (Latest LTS recommended)
- NPM (Comes with Node.js)
- Next.js Project (or create one)
Create a new Next.js project:
Install Electron and required packages:
bash
npm install electron electron-builder wait-on concurrently
-electron → Runs Electron
-electron-builder → Packages and builds the .exe file
-wait-on → Ensures Electron starts only after Next.js is ready
-concurrently → Runs multiple scripts in parallel
Create an electro.js file in the root directory (/electron.js):
const { app, BrowserWindow } = require("electron");
const path = require("path");
const isDev = process.env.NODE_ENV !== "production";
let mainWindow;
app.on("ready", () => {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
},
});
const appUrl = isDev
? "http://localhost:3000"
: `file://${path.join(__dirname, "out", "index.html")}`;
mainWindow.loadURL(appUrl);
if (isDev) {
mainWindow.webContents.openDevTools();
}
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
Edit package.json and add the following:
"main": "electron.js",
"scripts": {
"dev": "concurrently \"next dev\" \"wait-on http://localhost:3000 && electron .\"",
"build": "next build && next export",
"start": "next start",
"electron": "electron .",
"electron-pack": "electron-builder"
},
"build": {
"asar": true,
"files": [
"next.config.js",
"electron.js",
"package.json",
".next/**/*"
],
"win": {
"target": "nsis",
"icon": "public/icon.ico"
},
"mac": {
"target": "dmg",
"icon": "public/icon.icns"
}
}
Explanation "main": "electron.js" → Tells Electron which file to run. "asar": true → Packages source files securely. "win": { "target": "nsis" } → Builds a Windows installer (.exe). "mac": { "target": "dmg" } → Builds a macOS installer.
Run the following command:
npm run dev
If everything works, proceed to the next step.
Generate a static Next.js build:
npm run build
Run the following command:
npm run electron-pack
The .exe file will be generated inside the /dist folder.
You have successfully bundled a Next.js + Electron app into a Windows .exe file. 🚀
To build for macOS, modify the "mac" section in package.json and run the same command.
❓ Electron App Doesn’t Load in Production? If the production build doesn’t display anything, try modifying electron.js to load Next.js correctly:
const appUrl = isDev
? "http://localhost:3000"
: `file://${path.join(__dirname, "out", "index.html")}`;
Ensure next export is used to generate static files.
Now you have a Next.js desktop application packaged as a Windows executable. 🎊