Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Child Process Exit Codes in Node.js
📖 Scenario: You are building a Node.js script that runs a child process to execute a simple command. You want to check the exit code of the child process to know if it ran successfully or if there was an error.
🎯 Goal: Create a Node.js script that spawns a child process to run the ls command, captures its exit code, and prints a message based on whether the command succeeded or failed.
📋 What You'll Learn
Use the child_process module to spawn a child process
Run the ls command using spawn
Listen for the exit event to get the exit code
Print Success if exit code is 0, otherwise print Failure
💡 Why This Matters
🌍 Real World
Developers often run other programs from Node.js scripts and need to know if those programs finished successfully.
💼 Career
Understanding child process exit codes is important for building reliable automation scripts, deployment tools, and server-side applications.
Progress0 / 4 steps
1
Import the child_process module and create the child process
Write code to import spawn from the child_process module and create a child process called lsProcess that runs the ls command with no arguments.
Node.js
Hint
Use const { spawn } = require('child_process') to import spawn. Then call spawn('ls') to run the command.
2
Add a variable to hold the exit code
Create a variable called exitCode and set it to null. This will store the exit code of the child process.
Node.js
Hint
Use let exitCode = null to create the variable.
3
Listen for the exit event and save the exit code
Add an event listener on lsProcess for the exit event. Use (code) => as the callback parameter and assign code to the exitCode variable.
Node.js
Hint
Use lsProcess.on('exit', (code) => { exitCode = code; }) to capture the exit code.
4
Print success or failure based on the exit code
Add an event listener on lsProcess for the close event. Inside the callback, check if exitCode is 0. If yes, print Success. Otherwise, print Failure.
Node.js
Hint
Use lsProcess.on('close', () => { if (exitCode === 0) console.log('Success'); else console.log('Failure'); }).
Practice
(1/5)
1. What does an exit code of 0 from a Node.js child process usually mean?
easy
A. The process finished successfully without errors.
B. The process was terminated by a signal.
C. The process encountered an error and crashed.
D. The process is still running.
Solution
Step 1: Understand exit codes in Node.js child processes
Exit codes indicate how a process ended. Code 0 means success.
Step 2: Interpret exit code 0
Exit code 0 means the process finished without errors or interruptions.
Final Answer:
The process finished successfully without errors. -> Option A
Quick Check:
Exit code 0 = success [OK]
Hint: Exit code 0 always means success in child processes [OK]
Common Mistakes:
Confusing exit code 0 with error codes
Thinking signal termination returns 0
Assuming non-zero codes mean success
2. Which of the following is the correct way to listen for a child process exit event in Node.js?
easy
A. child.listen('exit', (code) => { /* handle exit */ });
But the console never logs anything. What is the likely problem?
medium
A. The exit event callback is missing the signal parameter.
B. The child process is not exiting because the event loop is blocked.
C. The child process exits after 1 second, but the main program ends before that.
D. The spawn command syntax is incorrect.
Solution
Step 1: Analyze the child process behavior
The child process exits after 1 second due to setTimeout.
Step 2: Consider the main program lifecycle
If the main program ends before 1 second, it may exit before child finishes, so no log appears.
Final Answer:
The child process exits after 1 second, but the main program ends before that. -> Option C
Quick Check:
Main program must stay alive to see child exit [OK]
Hint: Keep main program alive to catch delayed child exit events [OK]
Common Mistakes:
Thinking missing signal parameter breaks event
Assuming spawn syntax is wrong without error
Ignoring asynchronous timing of child exit
5. You want to run a child process and handle both normal exit and signal termination. Which code snippet correctly logs the exit code or signal received?
hard
A. child.on('exit', (code, signal) => {
if (code !== null) console.log(`Exited with code ${code}`);
else console.log(`Terminated by signal ${signal}`);
});
B. child.on('exit', (code) => {
if (code === 0) console.log('Success');
else console.log('Error');
});
C. child.on('close', (signal) => {
console.log(`Closed with signal ${signal}`);
});
D. child.on('exit', () => {
console.log('Process ended');
});
Solution
Step 1: Understand exit event parameters
The 'exit' event provides two parameters: code (number or null) and signal (string or null).
Step 2: Handle both exit code and signal
If code is not null, process ended normally; if null, it was terminated by a signal.
Step 3: Check code snippet correctness
child.on('exit', (code, signal) => {
if (code !== null) console.log(`Exited with code ${code}`);
else console.log(`Terminated by signal ${signal}`);
}); correctly checks code and signal and logs accordingly.
Final Answer:
child.on('exit', (code, signal) => {
if (code !== null) console.log(`Exited with code ${code}`);
else console.log(`Terminated by signal ${signal}`);
}); -> Option A
Quick Check:
Check code !== null to distinguish exit vs signal [OK]
Hint: Check if exit code is null to detect signal termination [OK]
Common Mistakes:
Ignoring the signal parameter in exit event
Using 'close' event instead of 'exit' for exit codes