简体   繁体   中英

(Node.js) How can I save in a global variable my converted .JSON file?

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);
    });

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM