We use process.exit to stop a Node.js program immediately. Exit codes tell the system if the program ended well or had a problem.
process.exit and exit codes in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
process.exit([code])
The code is a number. 0 means success, any other number means an error.
If you don't give a code, it defaults to 0.
process.exit()
process.exit(1)process.exit(42)This program prints a start message. If you run it with --fail argument, it prints an error and exits with code 1. Otherwise, it prints a success message and exits with code 0.
console.log('Start program'); if (process.argv.includes('--fail')) { console.error('Error: Something went wrong!'); process.exit(1); } console.log('Program finished successfully'); process.exit(0);
Always use process.exit carefully because it stops the program immediately, skipping any remaining code.
Exit codes help other programs or scripts know if your program worked or failed.
Use 0 for success and positive numbers for errors. Negative numbers are not recommended.
process.exit stops a Node.js program immediately.
Exit codes tell if the program ended well (0) or had an error (non-zero).
Use exit codes to communicate status to the system or other programs.
Practice
process.exit(0) do in a Node.js program?Solution
Step 1: Understand
process.exitbehaviorprocess.exitimmediately stops the Node.js program.Step 2: Interpret exit code
An exit code of00means the program ended successfully without errors.Final Answer:
Stops the program immediately and signals success -> Option AQuick Check:
Exit code 0 = success [OK]
- Thinking exit code 0 means error
- Confusing pause with exit
- Assuming process.exit restarts program
Solution
Step 1: Identify correct method to exit
The method to stop the program isprocess.exit()with a code inside parentheses.Step 2: Check syntax correctness
process.exit(1);is the correct syntax to exit with code 1. Other options misuse method or assignment.Final Answer:
process.exit(1); -> Option CQuick Check:
Correct method call syntax = process.exit(1); [OK]
- Swapping method and object names
- Assigning exit code instead of calling exit()
- Missing parentheses in method call
console.log('Start');
process.exit(2);
console.log('End');Solution
Step 1: Analyze the code flow
The script prints 'Start', then callsprocess.exit(2), which stops the program immediately.Step 2: Determine exit code and output
The exit code is2. The line printing 'End' never runs.Final Answer:
2 -> Option DQuick Check:
process.exit(2) sets exit code 2 [OK]
- Assuming all console.logs run
- Confusing exit code with default 0
- Thinking exit code 2 means success
process.exit = 1;
console.log('Exiting');
process.exit();Solution
Step 1: Check assignment to process.exit
The code assigns1toprocess.exit, replacing the function with a number.Step 2: Understand effect on function call
Callingprocess.exit()after overwriting causes an error because it's no longer a function.Final Answer:
process.exit is overwritten and no longer a function -> Option AQuick Check:
Overwriting process.exit breaks function call [OK]
- Thinking missing argument causes error
- Assuming console.log runs after exit
- Ignoring that process.exit is a function
import fs from 'fs';
const file = 'data.txt';
if (fs.existsSync(file)) {
process.exit(0);
} else {
process.exit(3);
}Solution
Step 1: Understand fs.existsSync usage
fs.existsSyncchecks synchronously if the file exists, returning true or false.Step 2: Check exit codes in if-else
If file exists,process.exit(0)runs; elseprocess.exit(3)runs. This matches the requirement.Final Answer:
Correctly exits with 0 if file exists, 3 if not -> Option BQuick Check:
Sync check + exit codes used correctly [OK]
- Using async fs.exists instead of sync
- Swapping exit codes in if-else
- Not calling process.exit with parentheses
