StreamThe process.stdout property returns a stream connected to
stdout (fd 1). It is a net.Socket (which is a Duplex
stream) unless fd 1 refers to a file, in which case it is
a Writable stream.
For example, to copy process.stdin to process.stdout:
import { stdin, stdout } from 'node:process'; stdin.pipe(stdout);
const { stdin, stdout } = require('node:process'); stdin.pipe(stdout);
process.stdout differs from other Node.js streams in important ways. See
note on process I/O for more information.
numberThis property refers to the value of underlying file descriptor of
process.stdout. The value is fixed at 1. In Worker threads,
this field does not exist.
process.stdout and process.stderr differ from other Node.js streams in
important ways:
- They are used internally by
console.log()andconsole.error(), respectively. - Writes may be synchronous depending on what the stream is connected to
and whether the system is Windows or POSIX:
- Files: synchronous on Windows and POSIX
- TTYs (Terminals): asynchronous on Windows, synchronous on POSIX
- Pipes (and sockets): synchronous on Windows, asynchronous on POSIX
These behaviors are partly for historical reasons, as changing them would create backward incompatibility, but they are also expected by some users.
Synchronous writes avoid problems such as output written with console.log() or
console.error() being unexpectedly interleaved, or not written at all if
process.exit() is called before an asynchronous write completes. See
process.exit() for more information.
Warning: Synchronous writes block the event loop until the write has completed. This can be near instantaneous in the case of output to a file, but under high system load, pipes that are not being read at the receiving end, or with slow terminals or file systems, it's possible for the event loop to be blocked often enough and long enough to have severe negative performance impacts. This may not be a problem when writing to an interactive terminal session, but consider this particularly careful when doing production logging to the process output streams.
To check if a stream is connected to a TTY context, check the isTTY
property.
For instance:
$ node -p "Boolean(process.stdin.isTTY)" true $ echo "foo" | node -p "Boolean(process.stdin.isTTY)" false $ node -p "Boolean(process.stdout.isTTY)" true $ node -p "Boolean(process.stdout.isTTY)" | cat false
See the TTY documentation for more information.