0
0
Node.jsframework~10 mins

Handling child process errors in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the child_process module.

Node.js
const { exec } = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bchild_process
Chttp
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated modules like 'fs' or 'http'.
Misspelling the module name.
2fill in blank
medium

Complete the code to execute a shell command using exec.

Node.js
exec('ls -l', (error, stdout, stderr) => {
  if ([1]) {
    console.error(`Error: ${error.message}`);
    return;
  }
  console.log(stdout);
});
Drag options to blanks, or click blank then click option'
Aerror
Bstdout
Cstderr
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Checking stdout or stderr instead of error.
Ignoring the error argument.
3fill in blank
hard

Fix the error in the code to properly listen for the 'error' event on a child process.

Node.js
const child = exec('node someScript.js');
child.on('[1]', (err) => {
  console.error('Child process error:', err);
});
Drag options to blanks, or click blank then click option'
Aerror
Bexit
Cclose
Ddisconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' or 'close' events to catch errors.
Not attaching an error listener.
4fill in blank
hard

Fill both blanks to handle errors and output from a spawned child process.

Node.js
const { spawn } = require('child_process');
const child = spawn('ls', ['-l']);
child.stdout.on('[1]', (data) => {
  console.log(`Output: ${data}`);
});
child.on('[2]', (err) => {
  console.error(`Error: ${err.message}`);
});
Drag options to blanks, or click blank then click option'
Adata
Berror
Cclose
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' or 'exit' to get output data.
Not listening to the 'error' event.
5fill in blank
hard

Fill all three blanks to create a child process, handle errors, and print output correctly.

Node.js
import { exec } from 'child_process';

exec('node -v', ([1], [2], [3]) => {
  if ([1]) {
    console.error(`Execution error: $[1].message}`);
    return;
  }
  console.log(`Node version: $[2]`);
});
Drag options to blanks, or click blank then click option'
Aerror
Bstdout
Cstderr
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of parameters.
Ignoring the error parameter.