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
3.491K
worr
3.168K
Related Tags
#io
#nodejs
#file
#npm
#module
#utility
#line
#readline
#lazy
#files
#lines
#javascript
#streaming
#stdin
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#