You want to run a custom executable ./script.sh with arguments arg1 and arg2 using execFile. Which code snippet correctly runs it and logs both output and errors safely?
hard📝 Application Q15 of 15
Node.js - Child Processes
You want to run a custom executable ./script.sh with arguments arg1 and arg2 using execFile. Which code snippet correctly runs it and logs both output and errors safely?
Arguments must be passed as an array separate from the executable path, so ['arg1', 'arg2'] is correct.
Step 2: Verify error and output handling
execFile('./script.sh', ['arg1', 'arg2'], (error, stdout, stderr) => {
if (error) {
console.error('Execution error:', error);
return;
}
if (stderr) {
console.error('Error output:', stderr);
}
console.log('Program output:', stdout);
}); checks for execution errors, logs stderr if present, and prints stdout, which is safe and complete.