← Back to Node.js, page 4

nodejs-0075 · Easy fresher

What is the difference between console.log, console.error, process.stdout and process.stderr, and can output be lost when a process exits?

console.log writes to standard output (stdout) and console.error writes to standard error (stderr). They are separate streams, so they can be redirected independently. Writes to pipes are asynchronous on some platforms, so calling process.exit() right after a large write can cut output. Set process.exitCode and let the process end naturally.

Full answer

Every process has three standard streams: stdin (input), stdout (normal output) and stderr (errors and diagnostics). In a terminal both outputs show on the screen, but in scripts and containers they are separate: node app.js > out.txt 2> err.txt sends them to different files.

  • console.log, console.info and console.table write to process.stdout. console.error, console.warn and console.trace write to process.stderr.
  • process.stdout.write(text) is the lower-level call. It does not add a newline, and returns false when the internal buffer is full (backpressure).
  • Format: console.log uses util.format, so %s, %d, %j and %o placeholders work.

Synchronous or not: files and TTYs are written synchronously. Pipes and sockets are synchronous on Linux, but asynchronous on Windows pipes in some cases (check the Node docs for the current rules). So process.exit() right after logging a lot can lose data when the output goes through an asynchronous pipe, for example a parent process reading your output.

Good habits:

  • Prefer process.exitCode = 1 and let the event loop empty, instead of process.exit(1).
  • In long-running services write logs to stdout and let the platform collect them (twelve-factor).
  • Send errors and diagnostics to stderr, data results to stdout, so command-line tools can be piped: node report.js | sort.
  • Use process.stdout.isTTY to detect a terminal, for example to disable colors when output is piped.
  • Do not log inside tight loops. Console output is slow compared with memory work.
javascript
const { spawnSync } = require('node:child_process');

const childCode = `
console.log('result: 42');                   // stdout
console.error('warning: slow disk');         // stderr
console.log('%s scored %d (%j)', 'Asha', 78, { grade: 'A' }); // format placeholders
process.stdout.write('no newline here');
process.stdout.write('\\n');
console.log('is a terminal:', Boolean(process.stdout.isTTY));
process.exitCode = 3;                        // exit with a code, without process.exit()
`;

const run = spawnSync(process.execPath, ['-e', childCode], { encoding: 'utf8' });

console.log('--- what the child wrote to stdout ---');
console.log(run.stdout.trimEnd());
console.log('--- what the child wrote to stderr ---');
console.log(run.stderr.trimEnd());
console.log('--- exit code ---');
console.log(run.status);

The parent captures the two streams separately, which is what a shell redirect or a log collector does. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): --- what the child wrote to stdout --- result: 42 Asha scored 78 ({"grade":"A"}) no newline here is a terminal: false --- what the child wrote to stderr --- warning: slow disk --- exit code --- 3

Key points

  • stdout is for data, stderr for errors and diagnostics.
  • console.log goes to stdout, console.error to stderr.
  • process.exit can cut pending output on pipes.
  • Use exitCode and a natural end where possible.

Common mistakes

  • Writing errors to stdout and breaking pipelines.
  • Calling process.exit() right after a big write.
  • Using console.log for high volume logging inside hot loops.
  • Printing colored text into files.

Follow-ups

  • Why does process.stdout.write return a boolean?
  • How can you redirect console output to a file in code?
  • What is the difference between a TTY and a pipe?

Tips

  • Say 'data to stdout, messages to stderr'.
  • Mention exitCode instead of process.exit.
  • Show the redirect example in words.