DEV Community

TenE
TenE

Posted on

1 1 1

Understanding err, stdout, and stderr in Node.js

When executing shell commands in Node.js using the child_process module, three critical elements shape the command's outcome: err, stdout, and stderr. Grasping their roles is vital for effective debugging and error handling. This guide dives into their differences and offers best practices to master them.


What is stdout?

stdout (Standard Output) is the stream where a command’s successful output is sent. It holds the expected results of a command, free of error messages.

Example: Capturing stdout

import { exec } from 'child_process';

exec('echo "Hello, World!"', (err, stdout, stderr) => {
  console.log(`STDOUT: ${stdout}`);
});
Enter fullscreen mode Exit fullscreen mode

Output:

STDOUT: Hello, World!
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • Contains the command’s standard output.
  • Excludes error messages (those are routed to stderr).

What is stderr?

stderr (Standard Error) is the stream dedicated to error messages produced by a command. It captures issues even if the command doesn’t completely fail.

Example: Capturing stderr

import { exec } from 'child_process';

exec('ls nonexistent-folder', (err, stdout, stderr) => {
  console.error(`STDERR: ${stderr}`);
});
Enter fullscreen mode Exit fullscreen mode

Output:

STDERR: ls: nonexistent-folder: No such file or directory
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • Captures error messages from the command.
  • May contain output even if err is null.

What is err?

The err argument in exec is a Node.js error object that delivers high-level details about a command’s failure, including:

  • Command failure messages
  • Exit codes
  • Execution errors (e.g., file not found, permission denied)

Example: Checking err

import { exec } from 'child_process';

exec('ls nonexistent-folder', (err, stdout, stderr) => {
  if (err) {
    console.error(`Error Message: ${err.message}`);
    console.error(`STDERR: ${stderr}`);
  }
});
Enter fullscreen mode Exit fullscreen mode

Output:

Error Message: Command failed: ls nonexistent-folder
STDERR: ls: nonexistent-folder: No such file or directory
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • Provides execution context, unlike stderr.
  • Appears only when the command fails to execute.
  • If a command runs but outputs an error, err may be null while stderr has content.

Using execSync: Synchronous Handling

With execSync, errors are thrown as exceptions rather than passed to a callback, requiring a different handling approach.

Example: Handling Errors with execSync

import { execSync } from 'child_process';

try {
  const output = execSync('ls nonexistent-folder').toString();
  console.log(output);
} catch (err) {
  console.error(`Caught Error: ${err.message}`);
  console.error(`STDERR Output: ${err.stderr.toString()}`);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Caught Error: Command failed: ls nonexistent-folder
STDERR Output: ls: nonexistent-folder: No such file or directory
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • Errors are thrown and must be caught with try/catch.
  • err.message offers high-level failure details.
  • err.stderr provides the command’s error output.

Key Differences: err vs. stdout vs. stderr

Feature stdout (Output) stderr (Error Output) err (Error Object)
Purpose Normal command output Error messages Node.js error details
When it’s used Successful output Command prints errors Command fails entirely
Exists if succeeds? ✅ Yes ❌ No ❌ No
Exists if fails? ❌ No ✅ Yes ✅ Yes

Best Practices for Error Handling

  • Always Check err First
  • Verify the command executed successfully before relying on stdout or stderr.
import { exec } from 'child_process';

exec('ls nonexistent-folder', (err, stdout, stderr) => {
  if (err) {
    console.error(`Error: ${err.message}`);
    return;
  }
  console.log(`Output: ${stdout}`);
});
Enter fullscreen mode Exit fullscreen mode
  • Use try/catch with execSync
  • Prevent crashes by wrapping synchronous calls in error handling.
import { execSync } from 'child_process';

try {
  const output = execSync('some-command').toString();
  console.log(output);
} catch (err) {
  console.error(`Error: ${err.message}`);
}
Enter fullscreen mode Exit fullscreen mode
  • Log stderr Separately
  • Keep normal output and errors distinct for better debugging.
import { exec } from 'child_process';

exec('some-command', (err, stdout, stderr) => {
  console.log(`STDOUT: ${stdout}`);
  console.error(`STDERR: ${stderr}`);
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering err, stdout, and stderr empowers you to handle command execution in Node.js with confidence:

  • Use stdout for normal output.
  • Use stderr for error messages.
  • Use err to manage execution failures.

By applying these concepts and best practices, you’ll create robust, error-resistant Node.js applications.

Happy coding! 🚀

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (2)

Collapse
 
nevodavid profile image
Nevo David

Finally understand the mess with err, stdout, and stderr - super helpful for my old Node scripts

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Stuff like this always helps me fix dumb mistakes way faster. Nice work.