Answer the question
In order to leave comments, you need to log in
How to write code for node js?
let obj;
fs.readFile("config.json", (err, data) => {
if (err){
console.log(err);
} else {
obj = JSON.Parse(data);
}
});
console.log(obj);
Answer the question
In order to leave comments, you need to log in
new Promise((resolve, reject) => {
fs.readFile("config.json", (err, data) => {
if(err) {
return reject(err);
}
resolve(JSON.Parse(data));
});
}).then(obj => {
console.log(obj);
// your code
}).catch(console.error);
Specifically, here it is easier to use synchronous reading of the file, and given that this is generally json, let obj = require('./config.json');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question