← All notes

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

Options and rules

See also

Claims

Sources

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

Permanent ID: iil:note:nagwqx79

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.},
}

Markdown

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.

All artifact files (JSON index)

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.