JS

HOW TO WRITE CSV FILES IN NODEJS

SEPARATE COLUMNS WITH , ADD NEW ROW WITH \R\N var csv = "Apple,Banana\r\nCherry,Durian";

WRITE & APPEND TO CSV

01

WRITE TO FILE const fs = require("fs"); fs.writeFileSync("demoA.csv", csv);

APPEND MORE  csv = "\r\nElderberry,Fig"; fs.appendFileSync("demoA.csv", csv);

DATA ARRAY var data = [   ["One", "Two"], ["Three", "Four"] ];

ARRAY TO CSV

02

WRITE TO FILE const stream = require("fs"). createWriteStream("FILE.CSV"); for (let i of data)  {   stream.write(i.join(",") + "\r\n"); } stream.end();