0
0
Node.jsframework~10 mins

exec for running shell commands in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - exec for running shell commands
Start
Call exec with command
Node.js runs shell command
Command executes in shell
Shell returns output or error
Callback receives error, stdout, stderr
Process output or handle error
End
This flow shows how Node.js exec runs a shell command, waits for it to finish, then returns output or error to the callback.
Execution Sample
Node.js
import { exec } from 'node:child_process';

exec('echo Hello', (error, stdout, stderr) => {
  if (error) {
    console.error(`Error: ${error.message}`);
  } else {
    console.log(`Output: ${stdout}`);
  }
});
This code runs the shell command 'echo Hello' and prints the output or error.
Execution Table
StepActionCommandCallback ParamsOutput/Effect
1Call exec'echo Hello'N/AStarts shell command execution
2Shell runs command'echo Hello'N/AShell prints 'Hello' to stdout
3Command finishes'echo Hello'error=null, stdout='Hello\n', stderr=''Callback invoked with output
4Callback processesN/Aerror=null, stdout='Hello\n', stderr=''Console logs: Output: Hello
5EndN/AN/AProgram ends
💡 Command completes successfully, callback receives output and no error.
Variable Tracker
VariableStartAfter exec callAfter command finishesFinal
errorundefinedundefinednullnull
stdoutundefinedundefinedHello\nHello\n
stderrundefinedundefined
Key Moments - 3 Insights
Why does the callback receive three parameters: error, stdout, and stderr?
Because exec runs a shell command that can produce output (stdout), errors (stderr), or execution errors (error). The callback handles all these to know what happened. See execution_table step 3.
What happens if the shell command fails?
If the command fails, the error parameter in the callback will contain details, and stdout may be empty. This lets you handle failures gracefully. See execution_table step 3 for error=null case; if error was set, you'd handle it in the callback.
Why is stdout 'Hello\n' and not just 'Hello'?
Because the shell command 'echo' adds a newline character at the end of its output. This is normal shell behavior. See execution_table step 3 and variable_tracker stdout value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of error passed to the callback?
Aundefined
Bnull
CAn Error object
DA string describing the error
💡 Hint
Check the 'Callback Params' column at step 3 in execution_table.
At which step does the shell command finish execution?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for when the callback is invoked with output in execution_table.
If the command was changed to 'invalidcommand', what would change in the execution_table?
Aerror would be null and stdout would have output
Bstderr would be empty and stdout would have error message
Cerror would contain an Error object and stdout would be empty
DCallback would not be called
💡 Hint
Recall how exec handles invalid commands and error parameter in callback.
Concept Snapshot
exec(command, callback) runs a shell command asynchronously.
Callback receives (error, stdout, stderr).
If error is null, command succeeded; stdout has output.
If error exists, command failed; handle error.
Use exec for simple shell commands in Node.js.
Full Transcript
This example shows how Node.js exec runs a shell command like 'echo Hello'. When exec is called, Node.js starts the shell command asynchronously. The shell runs the command and produces output or errors. When the command finishes, exec calls the callback with three parameters: error, stdout, and stderr. If the command succeeds, error is null and stdout contains the output, here 'Hello\n'. If the command fails, error contains details. The callback can then log the output or handle errors. This flow helps run shell commands from Node.js code easily.