0
0
Node.jsframework~20 mins

Handling child process errors in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Child Process Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a child process emits an 'error' event?
Consider this Node.js code snippet that spawns a child process. What will be logged if the child process fails to start due to an invalid command?
Node.js
import { spawn } from 'child_process';

const child = spawn('invalid_command');

child.on('error', (err) => {
  console.log('Error event:', err.message);
});

child.on('exit', (code) => {
  console.log('Exit event with code:', code);
});
ANo logs appear because the child process silently fails.
BLogs: 'Exit event with code: 1' and no error event is emitted.
CLogs both 'Error event: spawn invalid_command ENOENT' and 'Exit event with code: 0'.
DLogs: 'Error event: spawn invalid_command ENOENT' and no exit event is emitted.
Attempts:
2 left
💡 Hint
The 'error' event is emitted when the process cannot be spawned or killed.
state_output
intermediate
2:00remaining
What is the value of 'exitCode' after a child process error?
Given the following code, what will be the value of 'exitCode' after the child process emits an 'error' event?
Node.js
import { spawn } from 'child_process';

let exitCode = null;
const child = spawn('badcmd');

child.on('error', () => {
  exitCode = 'error_occurred';
});

child.on('exit', (code) => {
  exitCode = code;
});
A'error_occurred'
B0
Cnull
Dundefined
Attempts:
2 left
💡 Hint
Check which event sets the exitCode when the process fails to start.
📝 Syntax
advanced
2:00remaining
Which option correctly attaches an error handler to a child process?
Identify the correct syntax to handle errors from a spawned child process.
Node.js
import { spawn } from 'child_process';
const child = spawn('ls');
Achild.handle('error', (err) => console.error('Error:', err));
Bchild.error((err) => console.error('Error:', err));
Cchild.on('error', (err) => console.error('Error:', err));
Dchild.addEventListener('error', (err) => console.error('Error:', err));
Attempts:
2 left
💡 Hint
Node.js child processes use EventEmitter pattern.
🔧 Debug
advanced
2:00remaining
Why does this child process error handler never run?
Examine the code below. Why does the error handler never log anything even if the command is invalid?
Node.js
import { spawn } from 'child_process';

const child = spawn('invalidcmd');

child.on('exit', (code) => {
  console.log('Exited with code:', code);
});

child.on('close', (code) => {
  console.log('Closed with code:', code);
});
ABecause the 'exit' event only fires on successful commands.
BBecause there is no 'error' event listener attached to the child process.
CBecause 'close' event replaces the 'error' event.
DBecause spawn automatically retries invalid commands silently.
Attempts:
2 left
💡 Hint
Check if the code listens for the 'error' event.
🧠 Conceptual
expert
3:00remaining
What is the correct sequence of events when a child process fails to spawn?
Order the events emitted by a Node.js child process when the spawn command is invalid and fails immediately.
A2 only
B4, 2, 1, 3
C2, 4, 1, 3
D1, 3, 2, 4
Attempts:
2 left
💡 Hint
Consider which events fire when the process never starts.