From 98be90bb9efc6cddc38c34c68310b567cfaa616b Mon Sep 17 00:00:00 2001 From: upendrasingh Date: Sun, 22 Mar 2026 23:12:31 +0530 Subject: [PATCH] fix: support both development and production builds in electron Add intelligent path detection to support running Electron app after development builds (npm run build) in addition to production packages (npm run dist). Changes: - Check for development path first: process.cwd()/.output/server/index.mjs - Fallback to production path: app.asar.unpacked/.output/server/index.mjs - Show clear error messages if neither path exists - Enable server logs (stdio: inherit) in development mode - Show terminal window in development for debugging - Preserve original production behavior (hidden logs/terminal) This eliminates the need to run 'npm run dist' for every small change during development, significantly improving developer experience and iteration speed. Before: Developers had to package the entire app for testing After: Developers can test with 'npm run build && npm run electron' Fixes #284 Co-Authored-By: Claude Sonnet 4.5 --- electron/main.cjs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/electron/main.cjs b/electron/main.cjs index 6fd09bb..47148f1 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -39,10 +39,14 @@ function waitForServer(url) { }); } -// Start Nitro server (production) +// Start Nitro server (development or production) function startServer() { return new Promise((resolve) => { - const serverPath = path.join( + // Try development path first (after npm run build) + const devPath = path.join(process.cwd(), '.output', 'server', 'index.mjs'); + + // Fallback to production path (after npm run dist) + const prodPath = path.join( process.resourcesPath, 'app.asar.unpacked', '.output', @@ -50,11 +54,29 @@ function startServer() { 'index.mjs' ); - console.log("Starting server from:", serverPath); + // Determine which path to use + let serverPath; + let isDevelopment = false; + + if (fs.existsSync(devPath)) { + serverPath = devPath; + isDevelopment = true; + console.log("Starting server in DEVELOPMENT mode from:", serverPath); + } else if (fs.existsSync(prodPath)) { + serverPath = prodPath; + console.log("Starting server in PRODUCTION mode from:", serverPath); + } else { + console.error("ERROR: Server build not found!"); + console.error("Tried development path:", devPath); + console.error("Tried production path:", prodPath); + console.error("Please run 'npm run build' first."); + app.quit(); + return; + } serverProcess = spawn('node', [serverPath], { - stdio: 'ignore', // no terminal - windowsHide: true, // hide CMD + stdio: isDevelopment ? 'inherit' : 'ignore', // show logs in dev + windowsHide: !isDevelopment, // show terminal in dev env: { ...process.env, HOST: serverHost,