// Import 'child_process' module
const { exec } = require('child_process');
// Execute 'ls -l' command
exec('ls -l', (error, stdout, stderr) => {
// Check for errors
if (error) {
console.error('Error:', error);
return;
}
// Log the result
console.log('Result:', stdout);
});
code
Execute Shell Command using 'child_process' module
This Node.js code utilizes the 'child_process' module to execute the 'ls -l' shell command. It captures the result and prints it to the console. The comments provide explanations for each part of the code.
Node.js
Shell Command
child_process module
Execution