Reading Lines with readline
readline reads a stream one line at a time. Combine it with a file stream to process large text files.
Example
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const readline = require('node:readline');
const f = path.join(os.tmpdir(), 'r.txt');
fs.writeFileSync(f, 'a\nb\nc\n');
(async () => {
const rl = readline.createInterface({ input: fs.createReadStream(f) });
let n = 0;
for await (const line of rl) { n++; console.log(n, line); }
fs.unlinkSync(f);
})();Result
1 a 2 b 3 c
Exercise
Loop over lines.
for await (const l of )
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.