Last Updated: February 25, 2016
·
2.977K
· koenbollen

Iterate through lines of files with Node.js

fileinput

In Node.js it's not that trivial to simply iterate through a file by its lines. Especially when it's a large file or has long lines and you don't want everything to be loaded in memory first.

Introducing the fileinput module. Heavily based on the Python module which shares it name.

Installation

$ npm install fileinput

Usage

var fileinput = require('fileinput');

fileinput.input()
  .on('line', function(line) {
    console.log( fileinput.lineno(), line.toString('utf8') );
  });

It's as easy as that, it's fast, it's streaming and durable. More info in the module's README.md file.

Have fun!
—Koen