0
0
Node.jsframework~20 mins

execFile for running executables in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
execFile Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of execFile with callback
What will be logged to the console when this Node.js code runs?
Node.js
import { execFile } from 'node:child_process';

execFile('node', ['-e', 'console.log("Hello from execFile")'], (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 from execFile
BError: spawn node ENOENT
CStderr: Hello from execFile
DOutput: undefined
Attempts:
2 left
💡 Hint
Look at how execFile runs the node executable with inline code and how stdout is handled.
component_behavior
intermediate
1:30remaining
Behavior when executable path is incorrect
What happens when execFile is called with a non-existent executable path?
Node.js
import { execFile } from 'node:child_process';

execFile('/path/to/nonexistent', [], (error, stdout, stderr) => {
  if (error) {
    console.log(error.code);
  } else {
    console.log('No error');
  }
});
AENOENT
BEACCES
CNo error
DTypeError
Attempts:
2 left
💡 Hint
ENOENT means file or directory not found.
🔧 Debug
advanced
2:00remaining
Why does execFile callback never run?
Consider this code snippet. Why might the callback never be called?
Node.js
import { execFile } from 'node:child_process';

const child = execFile('node', ['-e', 'setTimeout(() => console.log("done"), 1000)']);

// No callback provided

console.log('Script finished');
AThe child process never starts without a callback
BexecFile requires a callback to run the child process
CThe child process runs but output is not captured or logged
DCallback is missing, so no code runs after child process ends
Attempts:
2 left
💡 Hint
execFile can run without a callback but output won't be handled.
📝 Syntax
advanced
1:30remaining
Correct usage of execFile with options
Which option correctly runs execFile with arguments and options to set working directory?
AexecFile('ls', (err, stdout) => { console.log(stdout); }, ['-l'], { cwd: '/tmp' });
BexecFile('ls', { cwd: '/tmp' }, ['-l'], (err, stdout) => { console.log(stdout); });
CexecFile('ls', ['-l'], (err, stdout) => { console.log(stdout); }, { cwd: '/tmp' });
DexecFile('ls', ['-l'], { cwd: '/tmp' }, (err, stdout) => { console.log(stdout); });
Attempts:
2 left
💡 Hint
Check the order of arguments: executable, args array, options object, callback.
🧠 Conceptual
expert
2:00remaining
Why prefer execFile over exec for running executables?
Which reason best explains why execFile is preferred over exec when running executables directly?
AexecFile supports streaming output, exec buffers all output in memory
BexecFile does not spawn a shell, so it avoids shell injection risks and is more efficient
CexecFile automatically retries on failure, exec does not
DexecFile can run scripts without specifying the interpreter, exec cannot
Attempts:
2 left
💡 Hint
Think about security and performance differences between exec and execFile.