I need to count the lines of a CSV file. If the file is empty, I want to write the columns names as first line, then the info that I need to log.
Any suggestions how to do this?
var i;
var count = 0;
require('fs').createReadStream(process.argv[2])
  .on('data', function(chunk) {
    for (i=0; i < chunk.length; ++i)
      if (chunk[i] == 10) count++;
  })
  .on('end', function() {
    console.log(count);
  });
This is a duplicate of Node.js: Count the number of lines in a file, please learn to google your problems first.
Quick answer: To count the lines of a CSV file you can use this.
fs.readFile('mfile.csv', function (err, data) {
    if (!err) {
        let lines = null, data.toString().split('\n')
    }
})
Detailed answer: If the line count is 0, then you can write the header data such as column name.
fs.readFile('mfile.csv', function (err, data) {
    if (!err) {
        let lines = null, data.toString().split('\n')
        if(lines <= 0) {
           fs.writeFileSync('mfile.csv', 'cloumn name')
           console.log('added column name')
        }
        console.log('lines ' + lines)
    }
})
