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.
process.stdout.write('no newline');
Try it
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.
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
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; setprocess.exitCodeand let the loop finish. write()adds nothing. Unlikeconsole.log(), there is no newline and no formatting. Add\nyourself.- Respect backpressure when writing large amounts: if
write()returnsfalse, wait forprocess.stdout.once('drain', ...). - Colour: check
process.stdout.hasColors()orisTTYbefore printing ANSI escape codes, and honour theNO_COLORenvironment variable. Piped output should be plain. - Progress bars: write
'\r'to return to the line start, or usereadline.cursorTo()andclearLine(). These only make sense on a TTY. - EPIPE: when a downstream program such as
headexits early, your writes fail. Handle'error'onprocess.stdoutand stop quietly. - Do not mix huge synchronous loops of
console.logwith latency-sensitive code; terminals are slow. - Replace for tests: you can temporarily wrap
process.stdout.writeto capture output; restore it afterwards.