Complete the code to import the child_process module.
const { exec } = require('[1]');The child_process module is used to create and control child processes in Node.js.
Complete the code to execute a shell command using exec.
exec('ls -l', (error, stdout, stderr) => { if ([1]) { console.error(`Error: ${error.message}`); return; } console.log(stdout); });
The error argument indicates if the command failed. Checking it helps handle errors.
Fix the error in the code to properly listen for the 'error' event on a child process.
const child = exec('node someScript.js'); child.on('[1]', (err) => { console.error('Child process error:', err); });
The 'error' event is emitted when the child process fails to start or has an error.
Fill both blanks to handle errors and output from a spawned child process.
const { spawn } = require('child_process');
const child = spawn('ls', ['-l']);
child.stdout.on('[1]', (data) => {
console.log(`Output: ${data}`);
});
child.on('[2]', (err) => {
console.error(`Error: ${err.message}`);
});The 'data' event on stdout streams output, and 'error' event handles errors.
Fill all three blanks to create a child process, handle errors, and print output correctly.
import { exec } from 'child_process'; exec('node -v', ([1], [2], [3]) => { if ([1]) { console.error(`Execution error: $[1].message}`); return; } console.log(`Node version: $[2]`); });
The callback parameters are error, stdout, and stderr. We check error first, then print stdout.