indexzero / nconf

Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
https://github.com/indexzero/nconf
MIT License
3.87k stars 252 forks source link

Can't load config json file properly #232

Open ssh-randy opened 8 years ago

ssh-randy commented 8 years ago

So I'm trying to follow the example closely, I have:

var config = require('nconf');
//priority order
//1. specific overrides 
config.overrides({
  'always': 'be this value'
});

//2. process.argv
//3. process.env
config.argv().env();

config.file('development', 'development.json');
console.log(config.get('nodeServer'))

module.exports = config;

Yet the output is always undefined. my json is defined as such:


{
  "nodeServer": "http://localhost:8090",
  "port": 8090,
}

and it's in the same directory as config.js. Any idea why this is occurring?

Also want to note, in my main server.js I have:

var config     = require('./config/config');
console.log(config.get('port'));

and that also returns undefined.

popenkomaksim commented 8 years ago

You have trailing comma in your development.json, after "port": 8090,

Following works like a charm:

{
  "nodeServer": "http://localhost:8090",
  "port": 8090
}
loremaps commented 7 years ago

Can you please try this: Instead of: config.file('development', 'development.json'); use: config.file('development', 'config/development.json');

popenkomaksim commented 7 years ago

@loremaps but, looks like development.json in the same directory as config.js

...and it's in the same directory as config.js

loremaps commented 7 years ago

@popenkomaksim indeed, I would expect this to work too. However, I run into this issue myself: my configuration file (json) is in the same folder as my config.js file (the directory was named "config" as well, under my project's root directory). If I do not use the path config/development.json I always get undefined same as @ssh-randy reported. I am not sure if this is a bug, as I started checking nconf today. However, inlcuding the folder name resolved the issue for me.