Electron+Express打包后出现Error: spawn node ENOENT错误求解决
spawn node ENOENT Error in Packaged Electron Apps Got it, let's break down why this is happening and how to fix it. When you package your Electron app with electron-packager, the standard node executable isn't available in the app's bundled environment—Electron includes its own Node.js runtime, but your code can't find it using just 'node' like it does when running in development.
Here are two solid solutions to get this working:
Solution 1: Use Electron's Built-in Node Executable Path
Instead of hardcoding 'node', use process.execPath—this points directly to the Node runtime embedded in your packaged Electron app. You also need to make sure your app.js path is correctly resolved (always use the path module to handle absolute paths, which avoids issues with bundled file structures):
const cp = require('child_process'); const path = require('path'); const { app, BrowserWindow } = require('electron'); // Resolve the absolute path to app.js and use Electron's Node runtime const appJsPath = path.join(__dirname, './app.js'); let instance = cp.spawn(process.execPath, [appJsPath]); app.on('ready', function(){ let appWindow = new BrowserWindow({ width: 1120, height: 620, webPreferences: { plugins: true }, icon: path.join(__dirname, '/public/icon/icon.png') }); appWindow.loadURL(`file://${path.join(__dirname, '/public/prva.html')}`); // appWindow.webContents.openDevTools(); }); app.on('window-all-closed', () => { app.quit(); });
Solution 2: Use child_process.fork() Instead of spawn()
Since you're spawning another Node.js script, fork() is actually a better fit here. It automatically uses the current Node environment (no need to specify the executable path) and comes with built-in IPC communication between parent and child processes:
const cp = require('child_process'); const path = require('path'); const { app, BrowserWindow } = require('electron'); // Fork the app.js script directly—handles Node path automatically let instance = cp.fork(path.join(__dirname, './app.js')); app.on('ready', function(){ let appWindow = new BrowserWindow({ width: 1120, height: 620, webPreferences: { plugins: true }, icon: path.join(__dirname, '/public/icon/icon.png') }); appWindow.loadURL(`file://${path.join(__dirname, '/public/prva.html')}`); // appWindow.webContents.openDevTools(); }); app.on('window-all-closed', () => { app.quit(); });
Quick Additional Checks
- Double-check that
app.jsis included in your packaged app—make sureelectron-packagerisn't excluding it via any ignore rules. - Always use
path.join()for file paths: this works across Windows, macOS, and Linux, and adapts correctly to the bundled app's file structure.
内容的提问来源于stack exchange,提问作者Miran Urbas

