Challenge - 5 Problems
execFile Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1: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()); });
Attempts:
2 left
💡 Hint
Look at how execFile runs the node executable with inline code and how stdout is handled.
✗ Incorrect
The execFile runs the node executable with the inline script printing 'Hello from execFile'. The callback receives stdout with that output. No error or stderr occurs.
❓ component_behavior
intermediate1: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'); } });
Attempts:
2 left
💡 Hint
ENOENT means file or directory not found.
✗ Incorrect
When the executable path does not exist, execFile returns an error with code 'ENOENT' indicating the file was not found.
🔧 Debug
advanced2: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');
Attempts:
2 left
💡 Hint
execFile can run without a callback but output won't be handled.
✗ Incorrect
Without a callback, execFile runs the child process but does not capture or log its output. The callback is optional.
📝 Syntax
advanced1:30remaining
Correct usage of execFile with options
Which option correctly runs execFile with arguments and options to set working directory?
Attempts:
2 left
💡 Hint
Check the order of arguments: executable, args array, options object, callback.
✗ Incorrect
execFile expects the arguments array as second, options object third, and callback last. Only option D follows this order.
🧠 Conceptual
expert2:00remaining
Why prefer execFile over exec for running executables?
Which reason best explains why execFile is preferred over exec when running executables directly?
Attempts:
2 left
💡 Hint
Think about security and performance differences between exec and execFile.
✗ Incorrect
execFile runs the executable directly without a shell, reducing risk of shell injection and improving performance. exec runs commands inside a shell.