问题
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!");
});
回答1:
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
来源:https://stackoverflow.com/questions/51639238/node-child-process-exits-immediately-after-packing-the-electron-app