How to Read a File Line By Line in Node.js

Here are three ways to read a file line by line in Node.js.

  1. Using readline
  2. Using line-reader
  3. Using n-readlines

For this tutorial, we will read the data.txt file line by line, which is in the root of my project folder.

J Robert Oppenheimer and Albert Einstien
hello world
Jawaan
Nun 2

Method 1: Using readline

The easiest way to read a file line by line in Node.js is to use the built-in “readline module.” This approach doesn’t require additional dependencies and is an excellent choice for most scenarios.

Here is a step-by-step guide.

Step 1: Import required modules

const fs = require('fs');

const readline = require('readline');

Step 2: Creating a read stream

You must create a read stream for the file you want to read line by line using the “fs.createReadStream()” method.

const readInterface = readline.createInterface({
  input: fs.createReadStream('data.txt'),
  console: false
});

Step 3: Handling events: line and close

Create a readline interface using the readline.createInterface() method and attach event listeners for the line and close events.

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

readInterface.on('close', function() {
  console.log('Finished reading the file.');
});

Here is a complete code with an output.

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

const readInterface = readline.createInterface({
  input: fs.createReadStream('data.txt'),
  console: false
});

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

readInterface.on('close', function() {
  console.log('Finished reading the file.');
});

Output

Read a File Line By Line in Node

Method 2: Using line-reader

The line-reader library offers an asynchronous API to read lines from a file, which can be especially handy when dealing large files. You can also use it with callbacks or as an iterable.

Installation

npm install line-reader --save

The line-reader module provides eachLine() method, which reads the file line by line.

Example

const lineReader = require('line-reader');

// Using callbacks
lineReader.eachLine('data.txt', (line, last) => {
  console.log(line);

  if (last) {
    console.log('Finished reading the file.');
    return false;
  }
});

Output

J Robert Oppenheimer and Albert Einstien
hello world
Jawaan
Nun 2
Finished reading the file.

Method 3: Using n-readlines

The n-readlines module is used to read files line by line without buffering the whole file in memory. It does this without using streams by reading the file’s content in chunks using Buffer and the native file system module.

Even though it works synchronously, it does not load the whole file in memory.

Here’s an essential guide on how to use the n-readlines module:

Step 1: Install the module

npm install n-readlines

Step 2: Use the module in your script

const NReadlines = require('n-readlines');

const liner = new NReadlines('data.txt');

let line;
while (line = liner.next()) {
  console.log(line.toString('ascii'));
}

Output

J Robert Oppenheimer and Albert Einstien
hello world
Jawaan
Nun 2

That’s it!

Leave a Comment