Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.stdin stream

Reference for process.stdin in Node.js: reading piped or typed input with async iteration, events or readline, TTY raw mode, and handling an empty stdin.

process.stdin is a readable stream connected to the standard input of your program. When a user types in a terminal, or another program pipes text into yours (cat data.txt | node app.js), the bytes arrive here.

It is an ordinary stream.Readable, so everything you know about streams applies: async iteration, 'data' and 'end' events, setEncoding(), and pipe(). For line-by-line input, wrap it in readline.

NODE
let text = '';
for await (const chunk of process.stdin) text += chunk;

Try it

The editor below has a "Program input" box; whatever is in it is delivered to process.stdin.

Node.js Reading everything

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 Line by line with readline

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.stdin                      // stream.Readable (net.Socket, fs.ReadStream or tty.ReadStream)
process.stdin.isTTY                // true only for an interactive terminal
process.stdin.setRawMode(flag)     // TTY only
process.stdin.fd                   // 0
Member Description
setEncoding(enc) Receive strings instead of Buffers.
on('data', fn) / on('end', fn) Event style reading. Adding a 'data' listener starts the flow.
isTTY true for a terminal, undefined when input is a pipe or file.
setRawMode(true) Deliver every key press at once, without line buffering or echo (terminals only).
pause() / resume() Stop and restart reading.

Returns: the property is a stream object; 'end' fires when the input is finished (end of the pipe, end of the file, or Ctrl+D in a terminal).

Errors / edge cases: setRawMode does not exist when stdin is not a TTY, so check process.stdin.isTTY first. If nothing is ever sent and stdin stays open, a read waits forever.

Usage notes

  • The stream type depends on where input comes from: a terminal gives a tty.ReadStream, a file redirect an fs.ReadStream, a pipe a net.Socket. Treat it as a Readable and the differences disappear.
  • Reading starts only when you ask. Until you attach a listener, call resume() or iterate, nothing is consumed. Once flowing, Node keeps the process alive until input ends or you call pause().
  • Chunks are not lines. A line can be split across two chunks. Use readline or collect the whole input before splitting.
  • Encoding matters. Call setEncoding('utf8') or decode Buffer chunks yourself; concatenating buffers as strings can break multi-byte characters.
  • Interactive prompts: use readline/promises (rl.question()) for a short question and answer session. Close the interface so the program can exit.
  • Detect piped input with !process.stdin.isTTY to choose between reading data and showing a prompt.
  • Empty or closed input simply ends the stream at once: the loops above finish with an empty string. Always handle that case.
  • Simple whole-file read: fs.readFileSync(0, 'utf8') reads file descriptor 0 synchronously, handy for competitive programming style scripts.