Child process exit codes tell you if a program finished successfully or had an error. They help you understand what happened after running another program inside your Node.js app.
Child process exit codes in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
childProcess.on('exit', (code, signal) => { // code is the exit code number or null // signal is the termination signal or null });
The code is a number where 0 means success and other numbers mean errors.
The signal tells if the process was stopped by a system signal like SIGTERM.
ls -lh command and logs the exit code when done.const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh']);
ls.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});const { exec } = require('child_process');
exec('node someScript.js', (error, stdout, stderr) => {
if (error) {
console.log(`Error code: ${error.code}`);
} else {
console.log('Script ran successfully');
}
});const { spawn } = require('child_process');
const proc = spawn('sleep', ['10']);
proc.kill('SIGTERM');
proc.on('exit', (code, signal) => {
console.log(`Exit code: ${code}, Signal: ${signal}`);
});This program runs a child Node.js process that exits with code 1. It listens for the exit event and prints the exit code. If the process was killed by a signal, it prints that instead.
import { spawn } from 'child_process'; const child = spawn('node', ['-e', "process.exit(1)"]); child.on('exit', (code, signal) => { if (code !== null) { console.log(`Child process exited with code ${code}`); } else if (signal !== null) { console.log(`Child process was killed by signal ${signal}`); } else { console.log('Child process exited'); } });
Exit code 0 means success; any other number usually means an error.
If the process is terminated by a signal, the exit code will be null and the signal will be set.
Always listen for the exit event to know when the child process finishes.
Child process exit codes tell if a process finished well or had errors.
Use the exit event to get the code and signal.
Code 0 means success; other codes or signals mean problems.
Practice
0 from a Node.js child process usually mean?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 AQuick Check:
Exit code 0 = success [OK]
- Confusing exit code 0 with error codes
- Thinking signal termination returns 0
- Assuming non-zero codes mean success
Solution
Step 1: Recall Node.js child process event syntax
Node.js child processes emit events listened withonmethod.Step 2: Confirm correct event and parameters
The 'exit' event useschild.on('exit', (code, signal) => {})syntax.Final Answer:
child.on('exit', (code, signal) => { /* handle exit */ }); -> Option DQuick Check:
Use child.on('exit', callback) [OK]
- Using non-existent methods like exit() or onExit()
- Using listen() instead of on()
- Missing parameters in the callback
const { spawn } = require('child_process');
const child = spawn('node', ['-e', "process.exit(5)"]);
child.on('exit', (code, signal) => {
console.log(`Exit code: ${code}, Signal: ${signal}`);
});What will be printed when this code runs?
Solution
Step 1: Understand the child process exit code
The child process runsprocess.exit(5), so it exits with code 5.Step 2: Check the exit event parameters
The exit event callback receives code 5 and signal null because it exited normally with code 5.Final Answer:
Exit code: 5, Signal: null -> Option BQuick Check:
process.exit(5) sets exit code 5 [OK]
- Assuming exit code is always 0
- Confusing signal with exit code
- Expecting signal to be set on normal exit
const { spawn } = require('child_process');
const child = spawn('node', ['-e', "setTimeout(() => process.exit(0), 1000)"]);
child.unref();
child.on('exit', (code) => {
console.log(`Exited with code ${code}`);
});But the console never logs anything. What is the likely problem?
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 CQuick Check:
Main program must stay alive to see child exit [OK]
- Thinking missing signal parameter breaks event
- Assuming spawn syntax is wrong without error
- Ignoring asynchronous timing of child exit
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 AQuick Check:
Check code !== null to distinguish exit vs signal [OK]
- Ignoring the signal parameter in exit event
- Using 'close' event instead of 'exit' for exit codes
- Not checking for null exit code before logging
