Open In App

How To Read a File Line By Line Using Node.js?

Last Updated : 15 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we’ll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package. Each method offers a simple, scalable solution for reading files line by line.

1. Using the Readline Module

Readline is a native module of Node.js, it was developed specifically for reading the content line by line from any readable stream. It can be used to read data from the command line. Since the module is the native module of Node.js, it doesn’t require any installation and can be imported as –

const readline = require('readline');

Since the readline module works only with Readable streams, we need to first create a readable stream using the fs module.

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

Now, listen for the line event on the file object. The event will trigger whenever a new line is read from the stream:

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

Example: Implementation to read a file using readline module.

Node
// Importing the Required Modules
const fs = require('fs');
const readline = require('readline');

// Creating a readable stream from file
// readline module reads line by line 
// but from a readable stream only.
const file = readline.createInterface({
    input: fs.createReadStream('gfg.txt'),
    output: process.stdout,
    terminal: false
});

// Printing the content of file line by
//  line to console by listening on the
// line event which will triggered
// whenever a new line is read from
// the stream
file.on('line', (line) => {
    console.log(line);
});


Output:

Read file with Readline Module

2. Using Line-reader Module

The line-reader module is an open-source module for reading file line by line in Node.js. It is not the native module, so you need to install it using npm(Node Package Manager) using the command:

npm install line-reader --save

The line-reader module provides eachLine() method which reads the file line by line. It had got a callback function which got two arguments: the line content and a boolean value that stores, whether the line read, was the last line of the file.

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

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

Example: Implementation to read a file using line reader module.

Node
// Importing required libraries
const lineReader = require('line-reader');

// eachLine() method call on gfg.txt
// It got a callback function
// Printing content of file line by line
// on the console
lineReader.eachLine('gfg.txt', (line, last) => {
    console.log(line);
});


Output:

Read file with Line-reader Module

Best Practices

  • Error Handling: Always handle errors appropriately, especially when dealing with file I/O operations.
  • Memory Management: For large files, consider using streaming or buffering techniques to avoid loading the entire file into memory at once.
  • Code Readability: Write clear and understandable code with appropriate comments to improve readability and maintainability.

Conclusion

Reading a file line by line in Node.js is a straightforward process, thanks to the built-in readline module and external libraries like readline-promise. By following the steps outlined in this guide, you can efficiently read and process large text files in your Node.js applications, ensuring optimal performance and scalability. Whether you prefer using core modules or external libraries, Node.js provides flexible options to meet your specific requirements for file handling tasks.



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: