0
0
Node.jsframework~20 mins

exec for running shell commands in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exec Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of exec with simple command
What will be the output of this Node.js code using exec to run a shell command?
Node.js
import { exec } from 'child_process';

exec('echo Hello World', (error, stdout, stderr) => {
  if (error) {
    console.error(`Error: ${error.message}`);
    return;
  }
  if (stderr) {
    console.error(`Stderr: ${stderr}`);
    return;
  }
  console.log(`Output: ${stdout.trim()}`);
});
AOutput: Hello World
BError: Command not found
COutput: echo Hello World
DStderr: Hello World
Attempts:
2 left
💡 Hint
Remember that exec runs the command and returns its output in stdout.
Predict Output
intermediate
1:30remaining
Handling errors in exec callback
What will this code print if the command does not exist?
Node.js
import { exec } from 'child_process';

exec('nonexistentcommand', (error, stdout, stderr) => {
  if (error) {
    console.log(`Error: ${error.message}`);
    return;
  }
  console.log(`Output: ${stdout}`);
});
AOutput: nonexistentcommand
BError: Command failed: nonexistentcommand
CError: spawn nonexistentcommand ENOENT
DOutput:
Attempts:
2 left
💡 Hint
If the command is not found, exec returns an error with a message about failure.
component_behavior
advanced
2:00remaining
Behavior of exec with large output
What happens if you run exec with a command that produces a very large output exceeding the default buffer size?
Node.js
import { exec } from 'child_process';

exec('yes | head -n 1000000', (error, stdout, stderr) => {
  if (error) {
    console.log(`Error: ${error.message}`);
    return;
  }
  console.log(`Output length: ${stdout.length}`);
});
AError: stdout maxBuffer exceeded
BNo output and no error
COutput length: 0
DOutput length: 100000
Attempts:
2 left
💡 Hint
exec has a default maxBuffer size for stdout and stderr.
📝 Syntax
advanced
1:30remaining
Correct syntax for exec import and usage
Which option shows the correct modern syntax to import and use exec from 'child_process' in Node.js ES modules?
A
import exec from 'child_process';
exec('ls', (err, stdout) => { console.log(stdout); });
B
const { exec } = require('child_process');
exec('ls', (err, stdout) => { console.log(stdout); });
C
import { exec } from 'child_process';
exec('ls').then(output => console.log(output));
D
import { exec } from 'child_process';
exec('ls', (err, stdout) => { console.log(stdout); });
Attempts:
2 left
💡 Hint
Node.js ES modules use import syntax with curly braces for named exports.
🔧 Debug
expert
2:00remaining
Why does this exec code not print output?
Consider this code snippet: import { exec } from 'child_process'; const result = exec('echo test'); console.log(result.stdout); Why does this code not print 'test'?
Node.js
import { exec } from 'child_process';

const result = exec('echo test');
console.log(result.stdout);
ABecause the command 'echo test' failed to run.
BBecause stdout is only available after the process exits, so result.stdout is undefined immediately.
CBecause exec returns a ChildProcess object, not the command output synchronously.
DBecause exec requires a callback to capture output, otherwise stdout is null.
Attempts:
2 left
💡 Hint
Check what exec returns and how output is accessed.