Last Updated: February 25, 2016
·
3.492K
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside