Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file one line at a time in node.js?

I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together.

 var Lazy=require("lazy");  new Lazy(process.stdin)      .lines      .forEach(           function(line) {                console.log(line.toString());            }  );  process.stdin.resume(); 

The bit that I'd like to figure out is how I might read one line at a time from a file instead of STDIN as in this sample.

I tried:

 fs.open('./VeryBigFile.csv', 'r', '0666', Process);   function Process(err, fd) {     if (err) throw err;     // DO lazy read   } 

but it's not working. I know that in a pinch I could fall back to using something like PHP, but I would like to figure this out.

I don't think the other answer would work as the file is much larger than the server I'm running it on has memory for.

like image 925
Alex C Avatar asked May 27 '11 18:05

Alex C


People also ask

How do I read a file line by line in NodeJS?

Method 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. const readline = require('readline');

What is the method to read a single line at a time from the file?

We can use java.io.BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached. Below is a simple program showing example for java read file line by line using BufferedReader.

Can help you to read one line each time from the file?

The readline() method helps to read just one line at a time, and it returns the first line from the file given. We will make use of readline() to read all the lines from the file given. To read all the lines from a given file, you can make use of Python readlines() function.


2 Answers

Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:

const fs = require('fs'); const readline = require('readline');  async function processLineByLine() {   const fileStream = fs.createReadStream('input.txt');    const rl = readline.createInterface({     input: fileStream,     crlfDelay: Infinity   });   // Note: we use the crlfDelay option to recognize all instances of CR LF   // ('\r\n') in input.txt as a single line break.    for await (const line of rl) {     // Each line in input.txt will be successively available here as `line`.     console.log(`Line from file: ${line}`);   } }  processLineByLine(); 

Or alternatively:

var lineReader = require('readline').createInterface({   input: require('fs').createReadStream('file.in') });  lineReader.on('line', function (line) {   console.log('Line from file:', line); }); 

The last line is read correctly (as of Node v0.12 or later), even if there is no final \n.

UPDATE: this example has been added to Node's API official documentation.

like image 104
Dan Dascalescu Avatar answered Sep 21 '22 06:09

Dan Dascalescu


For such a simple operation there shouldn't be any dependency on third-party modules. Go easy.

var fs = require('fs'),     readline = require('readline');  var rd = readline.createInterface({     input: fs.createReadStream('/path/to/file'),     output: process.stdout,     console: false });  rd.on('line', function(line) {     console.log(line); }); 
like image 37
kofrasa Avatar answered Sep 22 '22 06:09

kofrasa