I want to read properties from a config file - in a key value pair format from my main.js
I have a config file config.json
{
"IGUrl": "xyz",
"Key": "abc",
"dbName": "node-login"
}
I want to read the property say "IGUrl" from my app.js.
The code to access and read this file is
var config = require('./config.json');
var x = config.IGUrl;
console.log("TEST URL " + x)
But this is giving me an error Uncaught TypeError: Cannot read property 'IGUrl' of undefined
var { config } = require('./config.json')you are not importing correctly. If you would import correctly it will work. In config file you needmodule.exports = objectThatYouAreExportingJSON.parse()as well, but just tested this on my node and apparently requiring a json file automatically parses it. So your posted code works for me. Which version of node.js are you running since iirc, requiring a json file is a relatively recent addition.require()?config.jsonin same directory asmain.js