0

I'm trying to use child_process.fork() in my Electron application.

I'm using electron-builder. Everything seems to be working when running the application with the unpacked version. Once, the application is installed via NSIS the fork is not working at all. I checked all paths and they are fine.

I'm running the program on Windows as administrator.

The logs when running the installed app:

  • Script to be ran: C:\Program Files\electron-app\resources\data\src\index.js

  • Executable: Executable: C:\Program Files\electron-app\app.exe

The logs when running the unpacked app:

  • Script to be ran: D:\Workspace\electron-app\dist\win-unpacked\resources\data\src\index.js

  • Executable: D:\Workspace\electron-app\dist\win-unpacked\app.exe

The way I fork the child process in the main process is:

ipcMain.on(START, (event, config) => {
  if (isDev) {
    child = fork('./src/index.js', [config]);
  } else {
    const dataPath = path.join(process.resourcesPath, 'data');
    const childPath = path.join(dataPath, 'src');
    log.info('Trying to start...', childPath);
    log.info('Config: ', config);
    log.info('Executable: ', process.execPath);
    child = fork(`${childPath}/src/index.js`, [config]);
  }
});

And the source for the child process is copied into the extraResources folder on build:

"extraResources": [
      {
        "from": "./src/",
        "to": "data/src",
        "filter": [
          "**/*"
        ]
      }
    ]
kraikov
  • 53
  • 6
  • Please check if this is helpful: https://stackoverflow.com/questions/38172308/spawn-a-child-process-in-electron/57895111#57895111 – Ashish Ranjan May 14 '20 at 14:44
  • So I need to bundle the source I want to fork in `app.asar`? Can I do that with `electron-builder`? Also, if the source is inside `app.asar` how can I pass the path to the `fork` function? – kraikov May 14 '20 at 14:59
  • Yes, you would have to bundle the file as-is into `app.asar` this way would be able to refer it while creating a fork. Yes, I had used `electron-builder`. The path should be the same, probably you should build/bundle your project in such a way that both during development and while using it as an EXE, it stays at the same path. – Ashish Ranjan May 14 '20 at 15:44
  • Please note: mine was a portable executable. – Ashish Ranjan May 14 '20 at 15:46

0 Answers0