This program runs a small Node.js script as a child process that prints a message and exits with code 1. It listens for output, errors, and exit events to handle all cases.
Node.js
import { spawn } from'child_process';
// Try to run a command that may fail
const child = spawn('node', ['-e', 'console.log("Hello from child"); process.exit(1);']);
child.stdout.on('data', (data) => {
console.log(`Child output: ${data.toString().trim()}`);
});
child.on('error', (err) => {
console.error(`Failed to start child process: ${err.message}`);
});
child.on('exit', (code, signal) => {
if (code !== 0) {
console.log(`Child process exited with code ${code}`);
} else {
console.log('Child process exited successfully');
}
});
OutputSuccess
Important Notes
Always listen for the 'error' event to catch startup problems.
The exit code 0 means success; any other code usually means an error.
Use 'stdout' and 'stderr' streams to get output and error messages from the child process.
Summary
Handling child process errors helps your app stay stable when running other programs.
Listen to 'error' and 'exit' events to know what happens with the child process.
Check exit codes and output to understand success or failure.
Practice
(1/5)
1. What is the main reason to listen for the 'error' event when using Node.js child processes?
easy
A. To catch errors that happen when starting or running the child process
B. To get the output data from the child process
C. To close the child process manually
D. To restart the child process automatically
Solution
Step 1: Understand the purpose of the 'error' event
The 'error' event is triggered if the child process fails to start or encounters a problem during execution.
Step 2: Differentiate from other events
The 'exit' event tells when the process ends, but 'error' specifically catches startup or runtime errors.
Final Answer:
To catch errors that happen when starting or running the child process -> Option A
Quick Check:
'error' event = catch process startup/runtime errors [OK]
Hint: Remember: 'error' means process failed to start or crashed [OK]
Common Mistakes:
Confusing 'error' with 'exit' event
Thinking 'error' gives output data
Assuming 'error' restarts the process
2. Which of the following is the correct way to listen for errors on a child process created with spawn?
easy
A. child.on('error', (err) => { console.error(err); });
B. child.error((err) => { console.error(err); });
C. child.on('exit', (code) => { console.log(code); });
D. child.catch('error', (err) => { console.error(err); });
Solution
Step 1: Recall the correct event listener syntax
Node.js child processes use on method to listen for events like 'error'.
Step 2: Identify the correct event and method
child.on('error', (err) => { console.error(err); }); uses child.on('error', callback), which is the proper syntax to catch errors.
Final Answer:
child.on('error', (err) => { console.error(err); }); -> Option A
Quick Check:
Use on('error') to catch errors [OK]
Hint: Use child.on('error', callback) to catch errors [OK]