Bird
0
0

What will be the output of this code snippet?

medium📝 component behavior Q4 of 15
Node.js - Child Processes
What will be the output of this code snippet?
const { spawn } = require('child_process');
const child = spawn('nonexistentcommand');
child.on('error', (err) => console.log('Error:', err.message));
child.on('exit', (code) => console.log('Exit code:', code));
AError event is not triggered
BExit code: 1 Error: spawn nonexistentcommand ENOENT
CNo output, process runs silently
DError: spawn nonexistentcommand ENOENT Exit code: null
Step-by-Step Solution
Solution:
  1. Step 1: Understand spawn failure behavior

    Spawning a non-existent command triggers the 'error' event with ENOENT error.
  2. Step 2: Check event order and exit code

    The 'exit' event fires with code null because the process never started.
  3. Final Answer:

    Error: spawn nonexistentcommand ENOENT Exit code: null -> Option D
  4. Quick Check:

    Spawn error triggers 'error' event first [OK]
Quick Trick: Nonexistent commands trigger 'error' with ENOENT [OK]
Common Mistakes:
  • Expecting exit code 1 instead of null
  • Assuming no error event fires
  • Confusing event order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes