(DIR) Home
(DIR) Blog posts
Read a file one line at time using Node
13 September, 2015
QUOTE
This post was translated from HTML, inevitably some things will have changed or no longer apply - July 2025
END QUOTE
I found some better code on the StrongLoop site for reading a file one line at a time in Node.
It's something that should be in any scripting environment's standard library, but isn't in Node's, so before I lose the link, the StrongLoop article's [link no longer available] and that code as a gist is here.(Update: the code's below.)
Drop that module into your Node project, usage is then:
CODE
var readFileByLine = require('./readFileByLine');
function yourFunctionUsingOneLine(line) {
;
}
readFileByLine('yourFile', yourFunctionUsingOneLine);
END CODE
Update November 2023: The code, as I leave Github:
CODE
/*jslint node: true, nomen: true*/
'use strict';
// code from https://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/
var fs = require('fs'),
stream = require('stream'),
liner = new stream.Transform({ objectMode: true }),
readFileByLine;
// For Node 0.8
if (!stream.Transform) {
stream = require('readable-stream');
}
liner._transform = function (chunk, encoding, done) {
var data = chunk.toString(),
lines = '';
if (this._lastLineData) {
data = this._lastLineData + data;
}
lines = data.split('\n'); // skips blank lines
this._lastLineData = lines.splice(lines.length - 1, 1)[0];
lines.forEach(this.push.bind(this));
done();
};
liner._flush = function (done) {
if (this._lastLineData) {
this.push(this._lastLineData);
}
this._lastLineData = null;
done();
};
readFileByLine = function (fileName, doFunction) {
var source = fs.createReadStream(fileName);
source.pipe(liner);
liner.on('readable', function () {
var line = liner.read();
while (line) {
doFunction(line); // do something with the line
line = liner.read();
}
});
};
module.exports = readFileByLine;
END CODE