How to Print an Error Message with console.error in Node.js
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
console.error(<message>);
| Part | What it does |
|---|---|
console.error | Prints 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:

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:
node main.js
node main.js > report.txt
cat report.txt
Report saved.
Warning: 2 rows were skipped.
Warning: 2 rows were skipped.
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
| Mistake | What happens | Do this instead |
|---|---|---|
Printing warnings with console.log | They end up inside the redirected file, mixed with the results | Use console.error for problems |
Printing results with console.error | They don't reach the file or the next program in a pipe | Keep results on console.log |
Expecting console.error to stop the program | It only prints; the code carries on | Exit or throw after printing when the program can't continue |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Error messages are missing from a log file | Only standard output was redirected | Add 2> errors.txt to save standard error as well |
| A warning shows up inside saved data | It was printed with console.log | Switch 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
- console.error(), in the Node.js documentation.
- console.log(), in the Node.js documentation.
