Child_process.fork does not start the express server inside a packaged electronic application

I have an electronic application where I need to not only launch an interface for a user, but also launch an express server that will serve files for people connected via a network.

Everything works for me if I run both an electronic and an express server, but I'm sure that I will need a server running in a different thread to avoid a problematic interface and even server problems.

In this regard, I tried to start my express server using child_process.fork, and it worked when I use npm start , but when I use electron-builder to create .exe, the installed program does not start the express server.

I tried to start my server immediately using:

require('child_process').fork('app/server/mainServer.js')

I tried several changes, the __dirname file __dirname , process.resourcesPath and even hard-coding the generated file path; changing fork parameters for passing cwd: __dirname , detached: true and stdio: 'ignore' ; and even tried using spawn with process.execPath , which will also work with npm start , but will not be packaged (it will open new instances of my application, it seems obvious after you)

Note. If I don’t fork and do not require the script server immediately, using require('server/mainServer.js') , it works with the packaged application, so the problem is no longer an expression.

Note 2: I have asar: false to solve other problems, so there is no problem here.

I set up a small git project to show my problem:

https://github.com/victorivens05/electron-fork-error

Any help would be greatly appreciated.

+8
express child-process electron
source share
1 answer

With a lot of help, Samuel Attard ( https://github.com/MarshallOfSound ) I was able to solve the problem (he really solved for me)

As he said:

 the default electron app will launch the first file path provided to it so `electron path/to/thing` will work in a packaged state, that launch logic is not present it will always run the app you have packaged regardless of the CLI args passed to it you need to handle the argument manually yourself and launch that JS file if it passed in as the 1st argument The first argument to fork simply calls `process.execPath` with the first argument being the path provided afaik The issue is that when packaged Electron apps don't automatically run the path provided to them they run the app that is packaged within them 

In other words. fork is actually spawn executed with process.execPath and passes the first fork argument as the second to spawn.

What happens in a packaged application is that process.execPath not an electron, but a packaged application itself. Therefore, if you try spawn , the application will open again and again.

So, what Samuel suggested was implemented as follows:

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

Thus, the first time a packaged application is launched, process.argv[1] will be empty, so the server will not start. Then it will execute the electronic part (mainLocal in my case) and launch the application, but this time it will pass argv . The next time the application starts, it will start the server and stop execution, so the application will not open again because the caviar will never be reached.

Many thanks to Samuel.

+2
source share

All Articles