6

I have created an electron app which when run in the main process will spawn a node child process which will serve some html content.

app.on('ready', () => {
//check for port 80
const port = 80;
find('port', port)
    .then(function (list) {
        if (!list.length) {
            console.log('port 80 is free now');
            // in development show command prompt
            let options = {shell: true, detached: isDevelopment}, path = '';
            try {
                if (win32) { path = rccServerPathWin32 }
                else if(macOS){ path = rccServerPathMacOS }

                console.log(path)
                if (fs.existsSync(path)) {
                    serverProcess = child_process.execFile(path, ['/desktopmode'], options);
                    mainWindow = createMainWindow();

                    // Execute this once DOM is ready
                    socket = io(localRcc);
                    feathersjsApp = feathers();
                    feathersjsApp.configure(socketio(socket));
                } else {
                    mainWindow = createMainWindowErrorPage();
                }
            } catch (err) {
                console.error(err)
            }
        } else {
            console.log('%s is listening port 80', list[0]);
            // TODO Display page contains information what is currently occupied this port
            mainWindow = createMainWindowErrorPage();
        }
    })
})

I use the following settings to build and package the app:

  "scripts": {
      "start": "NODE_ENV=dev && electron .",
      "dist": "electron-builder",
      "dist-win": "electron-builder --win portable",
      "dist-mac": "electron-builder --mac",
      "dist-linux": "electron-builder --linux snap"
  },
  "build": {
      "extraResources": [{
          "from": "server",
          "to": "server"
      }]
  },

server is the directory where I have my node process, which I expect to be build in the Resources directory when I build the app for MacOS. Now the problem comes when I run the app. If I open the package and run the app from the executable file located in Contents/MacOS/'nameOfProgram' then it would start the app and run the child process. But if I try running the app from the package icon for example in Applications folder it won't start the child process. It starts a blank client and will not even show the default html for the electron app.

1 Answers1

3

Here's what solved this problem for me: https://stackoverflow.com/a/63257384/637173

I had two problems:

  1. I was using a relative path, which worked fine in development, but when the app was bundled the directory structure changed. Using app.getAppPath() or __dirname should resolve this

  2. I was using spawn(["node", "process.js"], which again worked fine in development, but in production there is no node process. The solution was just calling fork("process.js"), which uses Electron's internal node

bradjasper
  • 83
  • 1
  • 6