Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.stdout and process.stderr

Reference for process.stdout and process.stderr in Node.js: writing without a newline, isTTY, columns, backpressure, and why output may be lost on exit.

process.stdout and process.stderr are writable streams connected to your program's standard output and standard error. console.log() is a thin layer on top of process.stdout.write(), and console.error() writes to process.stderr.

Use them directly when you need control: printing without a trailing newline, writing raw bytes, drawing progress bars, or checking whether output goes to a person or to a pipe. Keep results on stdout and diagnostics on stderr, so that node app.js > out.txt captures only the data.

NODE
process.stdout.write('no newline');

Try it

Node.js Writing pieces and detecting a terminal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Callbacks and the two streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
process.stdout.write(chunk[, encoding][, callback])  // boolean
process.stdout.isTTY
process.stdout.columns / process.stdout.rows
process.stdout.getWindowSize()
process.stdout.hasColors([count])
process.stdout.getColorDepth()
process.stderr.write(chunk[, encoding][, callback])
Name Description
chunk A string, Buffer or Uint8Array to write.
encoding Text encoding for strings, default 'utf8'.
callback Called when the chunk has been handled (or on error).

Returns: write() returns true when you may keep writing, false when the internal buffer is full and you should wait for 'drain'.

Properties: isTTY is true on a terminal and undefined otherwise. columns, rows, getWindowSize(), hasColors() and getColorDepth() exist only on TTY streams and emit 'resize' when the window changes.

Errors / edge cases: writing after the stream has been closed raises ERR_STREAM_WRITE_AFTER_END or EPIPE (the reader went away, for example node app.js | head -1). Do not call end() or destroy() on these streams: they are shared by the whole process, and on a pipe a later write (even from console.log) can fail with EPIPE. When you are done, just stop writing.

Usage notes

  • Synchronous or not? Files and TTYs are written synchronously on Linux and macOS. Pipes are synchronous on Linux but asynchronous on Windows. Sockets are asynchronous. Therefore a large write followed by process.exit() can truncate the data; set process.exitCode and let the loop finish.
  • write() adds nothing. Unlike console.log(), there is no newline and no formatting. Add \n yourself.
  • Respect backpressure when writing large amounts: if write() returns false, wait for process.stdout.once('drain', ...).
  • Colour: check process.stdout.hasColors() or isTTY before printing ANSI escape codes, and honour the NO_COLOR environment variable. Piped output should be plain.
  • Progress bars: write '\r' to return to the line start, or use readline.cursorTo() and clearLine(). These only make sense on a TTY.
  • EPIPE: when a downstream program such as head exits early, your writes fail. Handle 'error' on process.stdout and stop quietly.
  • Do not mix huge synchronous loops of console.log with latency-sensitive code; terminals are slow.
  • Replace for tests: you can temporarily wrap process.stdout.write to capture output; restore it afterwards.