Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json2csv is not a function in nodejs

Tags:

node.js

I'm new to node.js help me to solve this question.. In node.js it is showing like json2csv is not a function .. But i installed all packages of json2csv ... in node cmd "npm install json2csv" i did ..

        var json2csv = require('json2csv');
        var fs = require('fs');
        var json = [
          {
            "car": "Audi",
            "price": 40000,
            "color": "blue"
          }, {
            "car": "BMW",
            "price": 35000,
            "color": "black"
          }, {
            "car": "Porsche",
            "price": 60000,
            "color": "green"
          }
        ];
        json2csv({data: json, fields: ['car', 'price', 'color']}, function(err, csv) {
          if (err) console.log(err);
          fs.writeFile('file.csv', csv, function(err) {
            if (err) throw err;
            console.log('file saved');
          });
        });
    
    
` json2csv({data: json, fields: ['car', 'price', 'color']}, function(err, csv) { if (err) console.log(err); fs.writeFile('file.csv', csv, function(err) { if (err) throw err; console.log('file saved'); }); });
like image 583
Bramhani G Avatar asked Feb 27 '18 10:02

Bramhani G


3 Answers

You can use this:

const json2csv = require('json2csv').parse;

const csv = json2csv(json, fields);
console.log(csv);
like image 96
Dale Nguyen Avatar answered Nov 18 '22 09:11

Dale Nguyen


Have you referred the documentation https://github.com/zemirco/json2csv#json2csv-parser-synchronous-api about how to use the api ?

like image 33
Jiby Jose Avatar answered Nov 18 '22 10:11

Jiby Jose


const json2csv = require('json2csv');

const csv = await json2csv.parse(json, fields);
console.log(csv);
like image 1
Jayesh Patil Avatar answered Nov 18 '22 10:11

Jayesh Patil