Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using spawn instead of exec
Trying to import the whole module without destructuring
Using execFile which is different
✗ Incorrect
The exec function is imported from the child_process module to run shell commands.
2fill in blank
mediumComplete 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'
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
✗ Incorrect
The command ls -l lists files in long format on Unix-like systems.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one parameter and missing stdout
Using wrong parameter names
Not handling stderr
✗ Incorrect
The callback for exec receives three parameters: error, stdout, and stderr.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong command
Using wrong variable name for output
Ignoring error handling
✗ Incorrect
The command is echo Hello and the output is in stdout.
5fill in blank
hardFill 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'
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
✗ Incorrect
The command cat file.txt reads the file content. The callback parameters are error and stdout for output.