3

I'm trying to follow the example here: https://www.npmjs.com/package/nconf

What I'm trying to do is load a configuration json file using nconf, but I can't seem to actually retrieve any of the configs. I've exported this to another file, but I've also tried just running this file directly too. 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.

Randy Song
  • 574
  • 1
  • 7
  • 22

3 Answers3

0

Turns out that I just needed this:

  config.file('development', 
    {file: 'config/development.json'});
Randy Song
  • 574
  • 1
  • 7
  • 22
0

Other users with similar issues should consider the current working directory in which their node app is executing.

In my situation (using Visual Studio Code), starting a debug session with the default configuration was not sufficient. I needed to add the cwd property to my launch configuration.

My VS Code launch.config now looks like:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}\\dist\\index.js",
            "preLaunchTask": "tsc: build - tsconfig.json",
            "outFiles": [
                "${workspaceFolder}/dist/**/*.js"
            ],
            "cwd": "${workspaceFolder}/dist/"
        }
    ]
}
MattEvansDev
  • 615
  • 7
  • 15
0
  • if your config folder is defined somewhere else other than projects root directory following should work fine.
config.file('development', `${__dirname}/development.json`)
  • if your config folder is in the projects root directory, then following code snippet should work fine
   config.file('development', 'config/development.json') 
   // should work fine