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
Written by Koen Bollen
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Io
Authors
koenbollen
2.976K
worr
2.626K
Related Tags
#io
#nodejs
#file
#npm
#module
#utility
#line
#readline
#lazy
#files
#lines
#javascript
#streaming
#stdin
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#