Skip to main content

How to Print an Error Message with console.error in Node.js

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A program has two output channels: standard output for its results and standard error for problems. console.log writes to the first and console.error to the second, which is why a warning printed with console.error still reaches you when the results go into a file.

Syntax​

Syntax
console.error(<message>);
PartWhat it does
console.errorPrints to standard error (stderr), followed by a new line.
<message>The text or value to print, as with console.log.

The Node.js console documentation: "Prints to stderr with newline." For console.log the same documentation says "Prints to stdout with newline."

The Example​

The example prints a result with console.log and a warning with console.error, then sends the output to a file to show which line goes where.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which prints Report saved. with console.log and Warning: 2 rows were skipped. with console.error.

main.js
console.log('Report saved.');
console.error('Warning: 2 rows were skipped.');

Step 2: Run It, Then Redirect the Output​

Run it normally, then send standard output to a file with > and look inside the file:

Terminal
node main.js
node main.js > report.txt
cat report.txt
Output
Report saved.
Warning: 2 rows were skipped.
Warning: 2 rows were skipped.
Report saved.

A zsh terminal where node main.js prints both lines, node main.js > report.txt prints only the warning, and cat report.txt shows Report saved.

The first run shows both lines. In the second, > sent standard output into report.txt, so only the warning, on standard error, stayed on screen; cat then shows the report line in the file. I ran the same three commands in PowerShell 7.6 and got the same result, since > redirects only standard output there too and on Windows cat is a PowerShell alias for Get-Content.

Common Mistakes​

MistakeWhat happensDo this instead
Printing warnings with console.logThey end up inside the redirected file, mixed with the resultsUse console.error for problems
Printing results with console.errorThey don't reach the file or the next program in a pipeKeep results on console.log
Expecting console.error to stop the programIt only prints; the code carries onExit or throw after printing when the program can't continue

Troubleshooting​

SymptomLikely causeFix
Error messages are missing from a log fileOnly standard output was redirectedAdd 2> errors.txt to save standard error as well
A warning shows up inside saved dataIt was printed with console.logSwitch that line to console.error

FAQ​

Does console.error print in red?​

No. Node.js prints it like any other text; the difference is the channel it goes to, not the color.

How do I save standard error to a file?​

Redirect it with 2>, as in node main.js 2> errors.txt. The same works in zsh, bash and PowerShell.

Conclusion​

console.log writes results to standard output and console.error writes problems to standard error. Redirect one and the other stays put, which is exactly what you want from a warning.

Additional Resources​