0
0
Node.jsframework~10 mins

exec for running shell commands 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 exec function from the child_process module.

Node.js
const { [1] } = require('child_process');
Drag options to blanks, or click blank then click option'
Afork
Bspawn
Cexec
DexecFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using spawn instead of exec
Trying to import the whole module without destructuring
Using execFile which is different
2fill in blank
medium

Complete the code to run the shell command 'ls -l' using exec.

Node.js
exec('[1]', (error, stdout, stderr) => {
  if (error) {
    console.error(`Error: ${error.message}`);
    return;
  }
  console.log(stdout);
});
Drag options to blanks, or click blank then click option'
Aecho Hello
Bls -l
Cpwd
Ddir
Attempts:
3 left
💡 Hint
Common Mistakes
Using Windows command 'dir' on Unix
Using 'pwd' which shows current directory only
Using 'echo Hello' which just prints text
3fill in blank
hard

Fix the error in the callback parameters to correctly handle exec output.

Node.js
exec('node -v', ([1]) => {
  if (error) {
    console.error(error);
    return;
  }
  console.log(stdout);
});
Drag options to blanks, or click blank then click option'
Aerror, stdout, stderr
Berr, output
Cerror
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one parameter and missing stdout
Using wrong parameter names
Not handling stderr
4fill in blank
hard

Fill both blanks to run 'echo Hello' and print the output or error.

Node.js
exec('[1]', (error, [2], stderr) => {
  if (error) {
    console.error(`Error: ${error.message}`);
    return;
  }
  console.log([2]);
});
Drag options to blanks, or click blank then click option'
Aecho Hello
Bstdout
Coutput
Dls
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong command
Using wrong variable name for output
Ignoring error handling
5fill in blank
hard

Fill all three blanks to run 'cat file.txt', handle errors, and print output.

Node.js
exec('[1]', ([2], [3], stderr) => {
  if ([2]) {
    console.error(`Error: $[2].message}`);
    return;
  }
  console.log([3]);
});
Drag options to blanks, or click blank then click option'
Acat file.txt
Berror
Cstdout
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong command like 'ls' instead of 'cat'
Mixing up error and output parameters
Not checking for error before printing output