Being able to read a file line by line provides us a chance to read the large files without loading them entirely in the memory. It also allows us to look for only the relevant information, and stop the search once we have found that information.

We have already discussed how to read a file line by line in Java, let us look at Node.js ways of reading a file line by line.

FS Module

The simplest way of reading a file line by line in Node.js is by using the native fs module's fs.readFileSync() method:

const fs = require('fs');

try {
    // read contents of the file
    const data = fs.readFileSync('file.txt', 'UTF-8');

    // split the contents by new line
    const lines = data.split(/\r?\n/);

    // print all lines
    lines.forEach((line) => {
        console.log(line);
    });
} catch (err) {
    console.error(err);
}

This method synchronously reads the entire contents of the file into the memory and then split its contents by line breaks. It looks perfect at first glance, but it has two problems:

  1. It is blocking which means that it will block the execution of the program until the entire file is loaded into the memory.
  2. It will have a severe impact on memory consumption if the file is very large (in gigabytes or more).

The first issue could be resolved by using the non-blocking version fs.readFile() but reading the entire file into memory is not something you want to do in a production environment.

However, if you want to read small files only, it works fine.

Readline Module

Readline is another Node.js native module that was developed specifically for this purpose — reading one line at a time from any readable stream. You can even use this module to read input data from command line.

Here is how you can access it in your code (no installation required):

const readline = require('readline');

Since readline module works with readable streams, we have to first create a stream by using the fs module like below:

const rl = readline.createInterface({
    input: fs.createReadStream('file.txt'),
    output: process.stdout,
    terminal: false
});

Now we can listen for the line event on rl object that will be triggered whenever a new line is read from the stream:

rl.on('line', (line) => {
    console.log(line);
});

Here is how the complete code looks like:

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

const rl = readline.createInterface({
    input: fs.createReadStream('file.txt'),
    output: process.stdout,
    terminal: false
});

rl.on('line', (line) => {
    console.log(line);
});

Line-Reader Module

line-reader is an open source module for reading a file line by line in Node.js. You can add it to your project by running the following command in your terminal:

$ npm i line-reader --save

The line-reader module provides eachLine() method that reads each line of the given file. It takes a callback function that is called with two arguments: the line content and a boolean value specifying whether the line read was the last line of the file. Here is an example:

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

lineReader.eachLine('file.txt', (line, last) => {
    console.log(line);
});

Another benefit of using this module is to stop the reading when some condition turns true. It can be achieved by returning false from the callback function:

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

lineReader.eachLine('file.txt', (line) => {
    console.log(line);

    // stop if line contains `NEW`
    if(line.includes('NEW')) {
        // stop reading and close the file
        return false;
    }
});

LineByLine Module

linebyline is another open-source library that can be used to read a file line by line in Node.js.

Let us add it your project:

$ npm i linebyline --save

This package simply streams the native readline module internally, reads and buffers new lines emitting a line event for each line:

const readline = require('linebyline');

// read all lines
rl = readline('file.txt');

// listen for `line` event
rl.on('line', (line, lineCount, byteCount) => {
    console.log(line);
}).on('error', (err) => {
    console.error(err);
});

Further Reading

If you enjoy reading this article, you may also like the below articles:

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.