0

I'm a beginners with Node.JS... I created a .Json file from a .csv with the 'csvtojson module' but I can't find out how save the Json in a global variable in order to use it... Could someone help me?

const CSVToJSON = require('csvtojson');
const fs = require('fs');

// convert users.csv file to JSON array
CSVToJSON().fromFile('flyers_data.csv')
    .then( flyers_data => {
        

        console.log(flyers_data);
    }).catch(err => {
        // log error if any
        console.log(err);
    });
  • Does this answer your question? [How to use global variable in node.js?](https://stackoverflow.com/questions/10987444/how-to-use-global-variable-in-node-js) – Matt Oct 15 '20 at 21:22
  • Why you need a global variable? – hoangdv Oct 16 '20 at 00:34

1 Answers1

0

in nodejs you can require .json file directly to use :

// foo.json


{
   "bar":"buzz"
} 

in js:

let fooFile =  require("/path/to/foo.json")


console.log(fooFile)
console.log(foorFile.bar)
Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46