4

This is my "app.js" file.

const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
const fs = require('fs')
const csv = require('csv-parser')
const randomWords = require('random-words')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];
const val = convertArrayToCSV(dataArrays, {
header,
separator: ','
});
console.log(val)

output:

number,first,last,handle
1,Mark,Otto,@mdo  
2,Jacob,Thornton,@fat
3,Larry,the Bird,@twitter

I want to save this "Val" as a .csv file on my device. How can I do that? Please help. I am stuck here for the last 4 hours.

4
  • filesystem fs library and there the function writeFile Commented Jan 13, 2021 at 14:11
  • I use writefile function but failed. Commented Jan 13, 2021 at 14:19
  • @Mujahidaul Islam as your convertArrayToCSV results in an string it should work Commented Jan 13, 2021 at 14:22
  • it could be that you need write access to the directory where you will create the file. You can check this in unix enviroment with ls -al Or do as path your home folder /home/newcsvfile.csv ~/newcsvfile.csv Commented Jan 13, 2021 at 14:24

2 Answers 2

3

File System Library

To achieve this you can use the File System Library of NodeJs

Data can be: <string> | <Buffer> | <TypedArray> | <DataView> | <Object>

As option you can parse an encoding like

fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);


const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
const fs = require('fs')
const csv = require('csv-parser')
const randomWords = require('random-words')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];
const val = convertArrayToCSV(dataArrays, {
header,
separator: ','
});
console.log(val)


fs.writeFile('<pathtodirectory>/message.csv', val, (err) => {
      if (err) throw err;
      console.log('The file has been saved!');
    });    
Sign up to request clarification or add additional context in comments.

3 Comments

You‘re welcome :) of you think this was helpful mark it as accepted, the button below up / down vote.
I don't enough reputation for voting. It needs a minimum of 15 reputations to vote.
Accepting is possible with any rep. You can choose one of your answers as helpful then other people see this helped solving the problem. Ist is the greyed out check below the up down button
2
const fs = require('fs')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];

const val = [header].concat(dataArrays).map(arr => arr.join(',')).join('\r\n');

fs.writeFile('filename.csv', val, err => {
  if(err) console.error(err);
  else console.log('Ok');
}););

1 Comment

Thank's for your time and effort .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.