Node.js readline - read a file or stdin line by line, and prompt for input
Answer: Create an interface with createInterface({ input }) and iterate it with for await (const line of rl). The node:readline module reads data from a Readable stream, such as process.stdin or a file stream, one line at a time; use node:readline/promises for await rl.question().
Examples
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';
const rl = createInterface({
input: createReadStream('input.txt'),
crlfDelay: Infinity,
});
for await (const line of rl) {
console.log(`Line: ${line}`);
}
import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
const prompt = readline.createInterface({ input, output });
const answer = await prompt.question('What do you think of Node.js? ');
console.log(`Thanks: ${answer}`);
prompt.close();
Events
'line'fires for each end-of-line input (\n,\r, or\r\n) on the input stream, and also when the stream ends without a final end-of-line marker.'close'is emitted whenrl.close()is called, when input receives its'end'event, or on Ctrl+D, or on Ctrl+C with no'SIGINT'listener.
Options and rules
crlfDelay: Infinitymakes\rfollowed by\nalways count as a single newline, which suits files with\r\nline delimiters. The default is100ms.- The application will not terminate until the interface is closed, because it waits for data on the
inputstream. - A
for await...ofloop that ends withbreak,throworreturncallsrl.close(), so iterating always consumes the input stream fully. - Errors in the input stream are not forwarded to the loop.
- Async iteration performance is not on par with the
'line'event API; userl.on('line', ...)for performance-sensitive code.
See also
Claims
- id: C1 claim: readline reads a Readable stream one line at a time. source: S2 evidence: "provides an interface for reading data from a [Readable][] stream"
- id: C2 claim: Async iteration is slower than the line event. source: S2 evidence: "Performance is not on par with the traditional
'line'event API."
- id: C3 claim: Errors in the input stream are not forwarded to for await loops. source: S1 evidence: "Errors in the input stream are not forwarded."
Sources
- [S1] Readline. Node.js API docs. https://nodejs.org/api/readline.html . License: MIT. Accessed 2026-11-18.
- [S2] doc/api/readline.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/readline.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Readline; nodejs/node (GitHub), doc/api/readline.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Readline; nodejs/node (GitHub), doc/api/readline.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.
Cite this
iinventedlight. 2026. "Node.js readline - read a file or stdin line by line, and prompt for input." iinventedlight Library, iil:note:nagwqx79, version of 2026-11-18. https://iinventedlight.com/library/notes/node-readline/. License: CC-BY-4.0.
@misc{iil_note_nagwqx79,
author = {{iinventedlight}},
title = {{Node.js readline - read a file or stdin line by line, and prompt for input}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/node-readline/},
note = {Permanent ID iil:note:nagwqx79. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- node-readline.table.json (table; CC-BY-4.0)
- node-readline.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-readline.md.