Read File Line By Line With Node.js

September 19, 2023 by Andreas Wik

It’s quick and easy to read a file line by line With Node.js.

First, import the modules needed.

const readline = require('readline');
const fs = require('fs');

 

Then create a readline interface and provide the file you want to read, in my case bickbeck-missp.dat).

const readInterface = readline.createInterface({
    input: fs.createReadStream('birkbeck-missp.dat'),
    output: process.stdout,
    console: false
});

 

The following lets you do whatever it is you need to for each line:

readInterface.on('line', function(line) {
    console.log(line);
});

 

That’s it, hope it helps!

Share this article

Recommended articles

@awikio