I am using the fork method to spawn a child process in my electron app, my code looks like this
'use strict'
 const fixPath = require('fix-path');
 let func = () => {
   fixPath();   
   const child = childProcess.fork('node /src/script.js --someFlags', 
   {
     detached: true, 
     stdio: 'ignore',
   }
 });
 child.on('error', (err) => {
   console.log("\n\t\tERROR: spawn failed! (" + err + ")");
 });
 child.stderr.on('data', function(data) {
   console.log('stdout: ' +data);
 });
 child.on('exit', (code, signal) => {
   console.log(code);
   console.log(signal);
 });
 child.unref();
But my child process exits immediately with exit code 1 and signal, Is there a way I can catch this error? When I use childprocess.exec method I can catch using stdout.on('error'... Is there a similar thing for fork method? If not any suggestions on how I can work around this?
Setting the option 'silent:true' and then using event handlers stderr.on() we can catch the error if any. Please check the sample code below:
 let func = () => {
   const child = childProcess.fork(path, args, 
   {
     silent: true,
     detached: true, 
     stdio: 'ignore',
   }
 });
 child.on('error', (err) => {
   console.log("\n\t\tERROR: spawn failed! (" + err + ")");
 });
 child.stderr.on('data', function(data) {
   console.log('stdout: ' +data);
 });
 child.on('exit', (code, signal) => {
   console.log(code);
   console.log(signal);
 });
 child.unref();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With