Write to a CSV in Node.js

Javascriptnode.jsCsv

Javascript Problem Overview


I am struggling to find a way to write data to a CSV in Node.js.

There are several CSV plugins available however they only 'write' to stdout.

Ideally I want to write on a row-by-row basis using a loop.

Javascript Solutions


Solution 1 - Javascript

You can use fs (https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback):

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

fs.writeFile('form-tracking/formList.csv', dataToWrite, 'utf8', function (err) {
  if (err) {
    console.log('Some error occured - file either not saved or corrupted file saved.');
  } else{
    console.log('It\'s saved!');
  }
});

Solution 2 - Javascript

The docs for node-csv-parser (npm install csv) specifically state that it can be used with streams (see fromStream, toStream). So it's not hard-coded to use stdout.

Several other CSV parsers also come up when you npm search csv -- you might want to look at them too.

Solution 3 - Javascript

Here is a simple example using csv-stringify to write a dataset that fits in memory to a csv file using fs.writeFile.

import stringify from 'csv-stringify';
import fs from 'fs';

let data = [];
let columns = {
  id: 'id',
  name: 'Name'
};

for (var i = 0; i < 10; i++) {
  data.push([i, 'Name ' + i]);
}

stringify(data, { header: true, columns: columns }, (err, output) => {
  if (err) throw err;
  fs.writeFile('my.csv', output, (err) => {
    if (err) throw err;
    console.log('my.csv saved.');
  });
});

Solution 4 - Javascript

If you want to use a loop as you say you can do something like this with Node fs:

let fs = require("fs")

let writeStream = fs.createWriteStream('/path/filename.csv')

someArrayOfObjects.forEach((someObject, index) => {		
	let newLine = []
	newLine.push(someObject.stringPropertyOne)
	newLine.push(someObject.stringPropertyTwo)
	....
	
	writeStream.write(newLine.join(',')+ '\n', () => {
		// a line was written to stream
	})
})

writeStream.end()

writeStream.on('finish', () => {
	console.log('finish write stream, moving along')
}).on('error', (err) => {
	console.log(err)
})

Solution 5 - Javascript

In case you don't wanna use any library besides fs, you can do it manually.

let fileString = ""
let separator = ","
let fileType = "csv"
let file = `fileExample.${fileType}`

Object.keys(jsonObject[0]).forEach(value=>fileString += `${value}${separator}`)
    fileString = fileString.slice(0, -1)
    fileString += "\n"

    jsonObject.forEach(transaction=>{
        Object.values(transaction).forEach(value=>fileString += `${value}${separator}`)
        fileString = fileString.slice(0, -1)
        fileString += "\n"
    })

fs.writeFileSync(file, fileString, 'utf8')

Solution 6 - Javascript

For those who prefer fast-csv:

const { writeToPath } = require('@fast-csv/format');

const path = `${__dirname}/people.csv`;
const data = [{ name: 'Stevie', id: 10 }, { name: 'Ray', id: 20 }];
const options = { headers: true, quoteColumns: true };

writeToPath(path, data, options)
        .on('error', err => console.error(err))
        .on('finish', () => console.log('Done writing.'));

Solution 7 - Javascript

**In case you don't wanna use any library besides fs, you can do it manually. More over you can filter the data as you want to write to CSV file **

router.get('/apiname', (req, res) => {
 const data = arrayOfObject; // you will get from somewhere
 /*
    // Modify old data (New Key Names)
    let modifiedData = data.map(({ oldKey1: newKey1, oldKey2: newKey2, ...rest }) => ({ newKey1, newKey2, ...rest }));
 */
 const path = './test'
 writeToFile(path, data, (result) => {
     // get the result from callback and process
     console.log(result) // success or error
   });
});

writeToFile = (path, data, callback) => {
    fs.writeFile(path, JSON.stringify(data, null, 2), (err) => { // JSON.stringify(data, null, 2) help you to write the data line by line
            if (!err) {
                callback('success');
                // successfull
            }
            else {
                 callback('error');
               // some error (catch this error)
            }
        });
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhil BottomleyView Question on Stackoverflow
Solution 1 - JavascriptJohn VandivierView Answer on Stackoverflow
Solution 2 - JavascriptJoe WhiteView Answer on Stackoverflow
Solution 3 - JavascriptcbaigorriView Answer on Stackoverflow
Solution 4 - JavascriptCentillionView Answer on Stackoverflow
Solution 5 - JavascriptGabriel BorgesView Answer on Stackoverflow
Solution 6 - JavascriptRon____View Answer on Stackoverflow
Solution 7 - JavascriptJyotirmoy UpadhayaView Answer on Stackoverflow