简体   繁体   中英

Node child process exits immediately after packing the electron app

I have this piece of code in the GUI part of my electron app which works perfectly alright when run from the terminal. I have packaged the app using 'electron-packager', then I started getting some issue.

Initially, the child process was terminating immediately and giving code 127 which I resolved by using 'fix-path' module as discussed here. https://github.com/electron/electron/issues/7688

Even after this, the process exits immediately with a code 1, I am unable to resolve this as there is no error getting reported. Is there a way to catch this exception/error once the child process exits?

const fixPath = require('fix-path');
let launch = () => {
fixPath();

const path = "SOME PATH";
var command = 'node ' + 
              path + 
              ' -d ' +      
              ' -e ' +     
              ' -r ' +      
              ' -p ' + 30 +
              ' -w ' +     
              ' -g ' +     
              '-server__ ';


const child = childProcess.exec(command, {
  detached: true,   
  stdio: 'ignore'
});

child.on('error', (err) => {
  console.log("\n\t\tERROR: spawn failed! (" + err + ")");
});

child.on('exit', (code, signal) => {
  console.log(code);
  console.log("\n\t\tGUI: spawned completed it's work!");
});

One can use child.stderr data event handler to catch the error. I added this piece of code in my script and I was able to debug the issue with the output on console.

child.stderr.on('data', function(data) {
  console.log('stdout: ' + data);
});

Refer this article which helped me to solve this issue. https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-know-e69498fe970a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM