Skip to main content

Mike Kreuzer

Read a file one line at time using Node

13 September 2015

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 and that ~~code as a gist is here~~. code is below.

Drop that module into your Node project, usage is then:

var readFileByLine = require('./readFileByLine');

function yourFunctionUsingOneLine(line) {
    ;
}

readFileByLine('yourFile', yourFunctionUsingOneLine);

Update November 2023: The code, as I leave Github:

/*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;