I use the following code to use config file and I want to read the values and I try with the following and I got error TypeError: undefined is not a function
This is the json file which I created in folder configuration/config
{
"ENV": {
"dev": {
"PORT": 3050
},
"prod": {
"PORT": 3060
}
}
}
config = require('./configuration/config');
I try like the following to get the ENV and I got error
var env = config.get('ENV');
I use this module
In debug I saw that the config is object with the defined values...
I use the following code to use config file and I want to read the values and I try with the following and I got error TypeError: undefined is not a function
This is the json file which I created in folder configuration/config
{
"ENV": {
"dev": {
"PORT": 3050
},
"prod": {
"PORT": 3060
}
}
}
config = require('./configuration/config');
I try like the following to get the ENV and I got error
var env = config.get('ENV');
I use this module https://www.npmjs./package/config
In debug I saw that the config is object with the defined values...
Share Improve this question edited Jul 1, 2015 at 15:31 John Jerrby asked Jul 1, 2015 at 15:26 John JerrbyJohn Jerrby 1,7037 gold badges34 silver badges69 bronze badges 5- 1 Should there by ma at "PORT": 3060, ? – aleksandar Commented Jul 1, 2015 at 15:28
- @wazaaaap- not this is not the problem the json is valid I just remove unnecessary properties for the post... – John Jerrby Commented Jul 1, 2015 at 15:29
- Package you are using uses main folder name in require() then filename.object for the get() function. – ODelibalta Commented Jul 1, 2015 at 15:31
- @Archer - sorry buy which URL? – John Jerrby Commented Jul 1, 2015 at 15:31
- It's okay - I deleted that ment as you added more info that made it irrelevant. – Reinstate Monica Cellio Commented Jul 1, 2015 at 15:31
3 Answers
Reset to default 4According to the module link you provided the config JSON (default.json
) needs to be in a folder off root called "config". You can then require
the module config using
var config = require('config');
and that appears to automatically load the JSON in and parse it.
You can then use the get
method like you did in your question:
var env = config.get('ENV');
You are using it wrong.
You need to have a "config/default.json", based on your root folder.
Then, just:
var config = require( 'config' );
var env = config.get( 'ENV' );
console.log( env );
config/default.json file:
{
"ENV": {
"dev": {
"PORT": 3050
},
"prod": {
"PORT": 3060
}
}
}
Just tested with you data, it works correctly.
EDIT:
I'm not sure if you need to use this module, but if you don't, you can just use something like this:
var config = require( './configuration/config.js' );
console.log( config.ENV );
And, your config file would be ./configuration/config.js (mind the .js not .json):
module.exports = {
"ENV": {
"dev": {
"PORT": 3050
},
"prod": {
"PORT": 3060
}
}
}
That's a simple, but efficient, config file.
You are using the module in the wrong way, you need to do this:
Put a folder called config
in your app folder, then you should create a file called default.json
inside that folder. So, it should look like this:
yourApp
--config
----default.json
the content of default.json
based in your example should be only this:
{
"ENV": {
"dev": {
"PORT": 3050
},
"prod": {
"PORT": 3060
}
}
}
and then, when you want to use your configuration anywhere you should require the module like this:
var config = require('config');
and then use your values like this:
var devPort = config.get("ENV.dev.port");
And sure, you can also do something like this:
var config = require('config');
var env = config.get('ENV.dev');
var port = env.port;
Remember that is a JSON
, you can play with the same way you can play with objects.