0

I have a Node app in a directory together with a text file called sample.csv.

I'm trying to read the file line-by-line with the following code, but nothing gets read:

var readline = require('readline');
var fs = require('fs');    

var lineReader = readline.createInterface({
   input: fs.createReadStream('sample.csv')
});

lineReader.on('line', function (line) {
    console.log(line); // Never happens
});

console.log('Completed.'); // Immediately skips to this

Any bright ideas? :) Thanks!

2
  • 1
    Check the example from npm readline page Commented Apr 6, 2016 at 8:07
  • Thanks for the tip, @Max! Commented Apr 6, 2016 at 8:13

1 Answer 1

3

Try this:

var readline = require('linebyline'),
        rl = readline('./sample.csv');
    rl.on('line', function (line, lineCount, byteCount) {
        console.log(lineCount, line, byteCount);
        // do something with the line of text
    })
    .on('error', function (e) {
            console.log("error", e);
        // something went wrong
    });

Node Cheat Available at line_by_line.

Sign up to request clarification or add additional context in comments.

2 Comments

Nevermind, I'm an idiot. I had a `process.exit()´ immediately after hooking up to the 'line' event... :( I'm guessing my original code probably would've worked, but regardless it seems 'linebyline' is the better option. Thanks again!
make sure file contains some csv content and it is in same dir, because I have just tested and added sample csv file in node-cheat link. If needed you can try that too. My sample sample_csv.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.