Append to a csv file in nodejs (using csv-write-stream)

Maybe late, but for people who encounter the same problem as me Error: no headers specified

  if (!fs.existsSync(finalPathFile))
    writer = csvWriter({ headers: ["header1", "header2"]});
  else
    writer = csvWriter({sendHeaders: false});

  writer.pipe(fs.createWriteStream(finalPathFile, {flags: 'a'}));
  writer.write({
    header1:"hello",
    header2:"world",
  });
  writer.end();

The concept stands for when you first access the file, you set the headers, so it can later be readable by other backend software like Excel. After that, when you later access the file again, you do not set the headers. But take in mind, each time you write to the file, you need to set the which column you're writing to. Then you'll get your desired result, which stands something like this:

header1,header2
John,5th avenue,
Richard,St. Jorge street

Actually, fs.createWriteStream function decides how to open 'out.csv'

In your case, you can open this file for adding by using a flag:

writer.pipe(fs.createWriteStream('out.csv', {flags: 'a'}))

Here are the docs for this function and flags.


I was having issues where the first row of data I tried to write after the header would be empty (just a bunch of commas like this ,,,,,).

I also was getting the Error: No headers message when I tried to set sendHeaders false.

My solution was based original @Firecat answer. I modified to this which works really well for me.

const fs = require('fs');
var csvWriter = require('csv-write-stream');
var writer = csvWriter({sendHeaders: false}); //Instantiate var
var csvFilename = "C:\some\path\myfile.csv";

// If CSV file does not exist, create it and add the headers
if (!fs.existsSync(csvFilename)) {
  writer = csvWriter({sendHeaders: false});
  writer.pipe(fs.createWriteStream(csvFilename));
  writer.write({
    header1: 'DATE',
    header2: 'LASTNAME',
    header3: 'FIRSTNAME'
  });
  writer.end();
} 

// Append some data to CSV the file    
writer = csvWriter({sendHeaders: false});
writer.pipe(fs.createWriteStream(csvFilename, {flags: 'a'}));
writer.write({
  header1: '2018-12-31',
  header2: 'Smith',
  header3: 'John'
});
writer.end();

// Append more data to CSV the file    
writer = csvWriter({sendHeaders: false});
writer.pipe(fs.createWriteStream(csvFilename, {flags: 'a'}));
writer.write({
  header1: '2019-01-01',
  header2: 'Jones',
  header3: 'Bob'
});
writer.end();