Node.js Reference ยท Streams and readline
Node.js readline line and close events
Reference for the readline Interface events in Node.js: line, close, pause, resume, history and SIGINT, with ordering rules and common mistakes.
A readline.Interface is an event emitter. The two events you will use most are 'line', emitted once for every complete line of input (without its line ending), and 'close', emitted when the interface is finished, normally because the input ended. Together they let you process input as a stream of lines and run cleanup at the end.
The other events report on the terminal state: 'pause' and 'resume' when input is paused or resumed, 'SIGINT' and 'SIGTSTP' when the user presses Ctrl+C or Ctrl+Z in a terminal, and 'history' when the history array changes.
rl.on('line', (line) => { /* one line of text */ }); rl.on('close', () => { /* input ended or rl.close() called */ });
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
rl.on('line', (input: string) => void) rl.on('close', () => void) rl.on('pause', () => void) rl.on('resume', () => void) rl.on('history', (history: string[]) => void) rl.on('SIGINT', () => void) rl.on('SIGTSTP', () => void) rl.on('SIGCONT', () => void)
| Event | When it is emitted |
|---|---|
line |
A line terminator (\n, \r\n or \r) was read, or the input ended with unterminated text. The argument is the line without its ending. |
close |
rl.close() was called, the input ended, or Ctrl+D was pressed (terminal mode). After close no more line events arrive. |
pause, resume |
Input paused or resumed, including automatically after close. |
history |
The history changed (terminal mode only, with historySize above 0). |
SIGINT |
Ctrl+C in terminal mode. If no listener exists, the interface pauses and the process gets its normal SIGINT. |
Returns: event listeners return nothing; the boolean rl.closed is true after close.
Errors: an exception thrown from a 'line' listener is an uncaught exception for the process. Handle failures inside the listener.
Usage notes
lineis synchronous per line. Async work started in the handler is not awaited, so lines are not throttled. For ordered, backpressure-aware processing usefor await.- Do not mix events and iteration on the same interface unless you know what you are doing: both read from the same internal buffer.
closefires once. Further calls torl.close()do nothing.- Input errors. An
errorevent on the input stream does not close the interface in all versions; also listen on the input. - Empty lines are events too. A blank line arrives as
''. - Final partial line. Text after the last newline is emitted as a final
'line'just before'close'. - Ctrl+C. Add a
SIGINTlistener to run shutdown code instead of letting the default action happen.