You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron+Express打包后出现Error: spawn node ENOENT错误求解决

Fixing the 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.js is included in your packaged app—make sure electron-packager isn'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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:03:04