child_process.fork not starting an express server inside of packaged electron app child_process.fork not starting an express server inside of packaged electron app express express

child_process.fork not starting an express server inside of packaged electron app


With the great help from Samuel Attard (https://github.com/MarshallOfSound) I was able to solve the problem (he solved for me actually)

As he said:

the default electron app will launch the first file path provided to itso `electron path/to/thing` will workin a packaged state, that launch logic is not presentit will always run the app you have packaged regardless of the CLI args passed to ityou need to handle the argument manually yourselfand launch that JS file if it's passed in as the 1st argumentThe first argument to fork simply calls `process.execPath` with the firstargument being the path provided afaikThe issue is that when packaged Electron apps don't automatically run thepath provided to themthey run the app that is packaged within them

In other words. fork is actually spawn being executed with process.execPath and passing the fork's first argument as the second for spawn.

What happens in a packaged app is that the process.execPath isn't electron but the packaged app itself. So if you try to spawn, the app will be open over and over again.

So, what Samuel suggest was implemented like this:

if (process.argv[1] === '--start-server') {   require('./server/mainServer.js')   return}require('./local/mainLocal.js')require('child_process').spawn(process.execPath, ['--start-server'])

That way, the first time the packaged app will be executed, the process.argv[1] will be empty, so the server won't start. It will then execute the electron part (mainLocal in my case) and start the app over, but this time passing the argv. Next time the app starts, it will start the server and stop the execution, so the app won't open again because spawn is never reached.

Huge thanks to Samuel.