
Subsequently, we log the line from the file available in the line variable. The second parameter is a callback function that has the line and the last variables. forEach ( line => MB ` ) įirst, we require the line reader module then call the eachLine function passing the filename (or file path) as the first parameter. readFileSync ( 'broadband.sql', 'utf-8' ) ĪllFileContents. Here is a quick example for reading the file line by line but in a not very performant sync way: const fs = require ( 'fs' ) Ĭonst allFileContents = fs. But, as we will load the whole file first before reading any lines from it the memory consumption will surely be more than 90 MB. We can possibly read the file in a synchronous way, meaning loading the whole 90 MB file in memory and loop through it. This should be a good test to look at how these ways perform for a relatively large file. We will also look at the memory consumption and the time it took to read the 90 MB file that has 798148 lines of text. The same file is used for each method of reading file line by line in Node.js to keep the test consistent across methods. The test file #įor all of the trail runs below we will use a 90 MB SQL dump file which I have taken from this BroadBandNow clone repository.
#Nodejs readline code
The code examples are available in a public GitHub repository for your convenience. In the following section we will look into the file we are going to use to read line by line with Node.js. I am running the code on a Mac with Node.js 14.
#Nodejs readline how to
Knowledge of how to install NPM modules would be necessary.

You can even use Node.js on docker for it.

With the ability to load and read a file line by line it enables us to stop the process at any step as per need. Reading the whole file at once will make the process memory intensive.

With the async path, it is possible to read large files without loading all the content of the file into memory. In Node.js files can be read in sync way or in an async way. There are multiple ways to read a file line by line with Node.js.
