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

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)
function(line) {

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:‘./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.


Solution 1:

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:

var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('')

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.

Solution 2:

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) {

Solution 3:

You don’t have to open the file, but instead, you have to create a ReadStream.


Then pass that stream to Lazy

Solution 4:

there is a very nice module for reading a file line by line, it’s called line-reader

with it you simply just write:

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

lineReader.eachLine('file.txt', function(line, last) {
  // do whatever you want with line...
    // or check if it's the last one

you can even iterate the file with a “java-style” interface, if you need more control:'file.txt', function(reader) {
  if (reader.hasNextLine()) {
    reader.nextLine(function(line) {

Solution 5:

require('fs').readFileSync('file.txt', 'utf-8').split(/\r?\n/).forEach(function(line){

Solution 6:

Old topic, but this works:

var rl = readline.createInterface({
      input : fs.createReadStream('/path/file.txt'),
      output: process.stdout,
      terminal: false
     console.log(line) //or parse line

Simple. No need for an external module.