How to insert new column into CSV in nodejs

1.1k Views Asked by At

I have to add a new cell in each row. let's say I have a list of products CSV and I am fetching the image URL for each product it using some functionality and I need to update the URL corresponding to each product.

const writestream = fs.createWriteStream('src/working_file.csv', {
    flags: 'a'
  });
 writestream.write({ image: imageURL });

the "image" is the new column name I want in my CSV file. "imageURL" is the URL of the produc I have in my hand

the above code is giving TypeError: Invalid non-string/buffer chunk

I got to read the CSV also for some reasons and I am using csv-parser for it.

1

There are 1 best solutions below

2
On

You need to add the imageURL as a string, not an object containing the imageURL. You might need to append a comma as well to separate it from the other columns, depending on the format of your file:

writestream.write("," + imageURL);