Introduction
We use exec to run commands on the computer's shell from a Node.js program. This helps us automate tasks or get system info.
Jump into concepts and practice - no test required
We use exec to run commands on the computer's shell from a Node.js program. This helps us automate tasks or get system info.
const { exec } = require('child_process');
exec('command', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Output: ${stdout}`);
});exec runs the command as a string in the shell.
The callback gets error, stdout (normal output), and stderr (error output).
const { exec } = require('child_process');
exec('ls', (error, stdout, stderr) => {
if (!error && !stderr) {
console.log(stdout);
}
});const { exec } = require('child_process');
exec('dir', (error, stdout, stderr) => {
if (!error && !stderr) {
console.log(stdout);
}
});const { exec } = require('child_process');
exec('node -v', (error, stdout, stderr) => {
if (!error && !stderr) {
console.log(`Node version: ${stdout.trim()}`);
}
});This program runs a simple shell command that prints a message. It shows how to handle errors and print the output.
const { exec } = require('child_process');
exec('echo Hello from shell', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Output: ${stdout.trim()}`);
});Always handle errors to avoid your program crashing.
Be careful with user input in commands to avoid security risks.
exec runs commands asynchronously, so your program keeps running while waiting.
exec lets Node.js run shell commands easily.
Use it to automate or get info from the system.
Always check for errors and output carefully.
exec function in Node.js primarily do?exec function is designed to run shell commands from Node.js code.exec.exec from the child_process module in Node.js?exec is imported as const exec = require('child_process').exec;.import exec from 'child_process'; uses ES module default import incorrectly; const exec = require('exec'); tries to require a non-existent module; import { exec } from 'child_process'; is ES module named import but Node.js needs special config.test.txt?
const { exec } = require('child_process');
exec('ls', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Output: ${stdout}`);
});ls lists files in the current directory. If test.txt exists, it will appear in the output.exec:
const { exec } = require('child_process');
exec('node -v', (error, stdout, stderr) => {
if (error) {
console.log(error);
}
console.log(stdout);
});exec usage correctly achieves this in Node.js?| sends output of ls to grep log to filter lines containing 'log'.ls | grep log correctly pipes output to grep for filtering; ls > grep log uses redirection to create a file; ls && grep log runs sequentially without piping; ls | find log pipes to filesystem search tool unsuitable for text filtering.