Standard library / stdio streams · on the one-page reference
stdio streams
Experimental — new, manually tested rather than conformance-suite covered.
process.stdin()/stdout()/
stderr() reuse the exact ReadableStream/
WritableStream types
fs.createReadStream/createWriteStream return,
just wired to the process's real stdio file descriptors instead of an
opened file — every method below (.read()/
.close()/.write()) already works on them.
stdout()/stderr() flush after every
.write() call (unlike a file-backed WritableStream,
which still buffers until .close()), so writes interleave
correctly, in order, with console.log in the same run.
process.stdin() Experimental
→ ReadableStream — each call allocates a
fresh wrapper around the same real stdin file descriptor (no shared/
cached instance). .read() (64KB chunks) works exactly as it
does on any other ReadableStream; see
ReadableStream.readLine() below for line-oriented reading.
const input = process.stdin();
const chunk: string = input.read();
console.log(chunk);
process.stdout() Experimental
→ WritableStream — .write(chunk)
flushes immediately, every call, so it interleaves correctly with
console.log. Closing it (.close()) closes the
real stdout fd — usually not something a program needs to do
before it exits.
console.log("before");
process.stdout().write("interleaved\n");
console.log("after");
process.stderr() Experimental
→ WritableStream — same shape as
process.stdout(), wired to fd 2 instead of fd 1.
process.stderr().write("warning: low disk space\n");
ReadableStream.readLine() Experimental
→ string — available on any
ReadableStream (process.stdin() or
fs.createReadStream(...) alike). Returns the next line
with its trailing \n/\r\n kept, not
stripped — deliberate: stripping it would make a
genuinely blank line indistinguishable from real end-of-stream (both
would be ""), silently truncating a
while (line != "") read loop on ordinary blank input
lines. "" means true EOF only; call .trim()
for a stripped line. A final line with no trailing newline (a piped
input cut off mid-line) is still returned intact, not dropped.
const input = process.stdin();
while (true) {
const raw: string = input.readLine();
if (raw == "") {
break;
}
console.log(raw.trim());
}