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.
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.
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.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 anfs.ReadStream, a pipe anet.Socket. Treat it as aReadableand 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 callpause(). - Chunks are not lines. A line can be split across two chunks. Use
readlineor collect the whole input before splitting. - Encoding matters. Call
setEncoding('utf8')or decodeBufferchunks 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.isTTYto 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.