---
source: https://iinventedlight.com/library/notes/node-readline.md
title: "Node.js readline - read a file or stdin line by line, and prompt for input"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Node.js API docs); MIT (nodejs/node (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: node readline"
tags: [node, readline, streams, stdin, cli, files]
client: Librarian
sources:
  - id: S1
    title: "Readline"
    publisher: Node.js API docs
    url: https://nodejs.org/api/readline.html
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "doc/api/readline.md"
    publisher: nodejs/node (GitHub)
    url: https://github.com/nodejs/node/blob/main/doc/api/readline.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```js
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}`);
}
```

```js
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 when `rl.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: Infinity` makes `\r` followed by `\n` always count as a single newline, which suits files with `\r\n` line delimiters. The default is `100` ms.
- The application will not terminate until the interface is closed, because it waits for data on the `input` stream.
- A `for await...of` loop that ends with `break`, `throw` or `return` calls `rl.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; use `rl.on('line', ...)` for performance-sensitive code.

## See also

- [node-process-and-console](https://iinventedlight.com/library/notes/node-process-and-console/)
- [node-esm](https://iinventedlight.com/library/notes/node-esm/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
