0
0
Node.jsframework~10 mins

Child process exit codes in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Child process exit codes
Start parent process
Spawn child process
Child runs task
Child finishes
Child sends exit code
Parent receives exit code
Parent handles exit code
End
The parent process starts a child process, which runs a task and then exits with a code. The parent receives this code and can act based on it.
Execution Sample
Node.js
const { spawn } = require('child_process');
const child = spawn('node', ['-e', 'process.exit(1)']);
child.on('exit', (code) => {
  console.log(`Child exited with code ${code}`);
});
This code spawns a child Node.js process that immediately exits with code 1, and the parent logs the exit code.
Execution Table
StepActionChild Process StateExit CodeParent Reaction
1Parent spawns child processRunningN/AWaiting for exit
2Child runs code: process.exit(1)Exiting1Receives exit event
3Parent receives exit eventExited1Logs: Child exited with code 1
4Parent finishes handlingExited1Process ends or continues
💡 Child process exits with code 1, parent receives and logs it.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
childundefinedChild process object (running)Child process object (exiting)Child process object (exited)Child process object (exited)
codeundefinedundefined111
Key Moments - 2 Insights
Why does the exit code appear only after the child process finishes?
Because the exit code is sent when the child process ends, as shown in execution_table step 2 and 3 where the parent waits until the child exits to get the code.
Can the exit code be anything other than 0 or 1?
Yes, exit codes can be any integer between 0 and 255, where 0 means success and others indicate different errors or statuses, as the child sets it before exiting (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the child process state at step 2?
ARunning
BExited
CExiting
DNot started
💡 Hint
Check the 'Child Process State' column at step 2 in the execution_table.
At which step does the parent receive the exit code from the child?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for when the parent logs the exit code in the execution_table.
If the child process exited with code 0 instead of 1, what would change in the execution_table?
AThe 'Exit Code' column would show 0 instead of 1
BThe 'Child Process State' would change to Running
CThe parent would not receive an exit event
DThe parent would spawn a new child process
💡 Hint
Focus on the 'Exit Code' column values in the execution_table.
Concept Snapshot
Child processes run separately from the parent.
They exit with a code (0 means success, others mean errors).
Parent listens for 'exit' event to get this code.
Use exit codes to know if child succeeded or failed.
Example: process.exit(1) exits with code 1.
Full Transcript
In Node.js, a parent process can create a child process to run tasks separately. The child process runs its code and then exits with a number called an exit code. Zero means success, any other number means some error or status. The parent listens for the child's exit event to get this code and can then decide what to do next. This flow helps the parent know if the child finished well or had problems.