1

I am looking at this https://github.com/lorenwest/node-config, and it seems to do everything I need. However I am wondering if it's possible to specify multiple config files based on their categories.

Is this possible to do this?

./config
   default-aws.json or aws-default.json
   production-aws.json or aws-production.json
   db-default.json
   db-production.json 

etc..

so the config files can be smaller? I know we could make a giant config that has all of those required in different sections. eg

{
   "aws": {
      "kinesis": "my-stream"
       ....
    },
    "db": {
       "host": "my-host"
        ...
    }
}

Anyone has any ideas if this is doable using node-config or different library that works similar to node-config?

Thanks & regards Tin

Tin Ng
  • 937
  • 2
  • 11
  • 25

3 Answers3

3

The short answer is NO. node-config doesn't support this (As @Mark's response).

The simplest way to do this using node-config is to use JavaScript as config files (https://github.com/lorenwest/node-config/wiki/Configuration-Files#javascript-module---js). This way you still get most of the benefits of using node-config. Then you can simply include other files inside them (https://github.com/lorenwest/node-config/wiki/Special-features-for-JavaScript-configuration-files#including-other-files)

Note that it would be a bit harder to use the multiple config files and overrides for the inner files.

A slightly more complex implementation can use the utility class in config which actually allows you to directly read a specific folder using the same patterns that node-config uses internally (https://github.com/lorenwest/node-config/wiki/Using-Config-Utilities#loadfileconfigsdirectory). In that case you would probably want to combine all the files to a single config by setting them on the config object before the first call to get (https://github.com/lorenwest/node-config/wiki/Configuring-from-an-External-Source).

Gabi Lee
  • 1,193
  • 8
  • 13
3

I'm a maintainer of node-config. The next version will support a feature to declare multiple NODE_ENV values, separated by a comma.

So if you were doing development in the cloud, you could declare a "development" environment followed by a "cloud" environment.

First the development config would be loaded, followed by the "cloud" config.

The related pull request is #486.

Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
2

I use nconf. It lets you read multiple configuration files into the same object. Here is an example:

var nconf = require('nconf');

//read the config files; first parameter is a required key
nconf.file('aws', {file: 'default-aws.json'});
nconf.file('db', {file: 'db-default.json'});

console.log(nconf.get('aws:kinesis'));
console.log(nconf.get('db:host'));

default-aws.json:

{
  "aws": {
    "kinesis": "my-stream"
  }
}

db-default.json:

{
  "db": {
    "host": "my-host"
  }
}
Edwin Torres
  • 2,774
  • 1
  • 13
  • 15
  • Thank you. How would you get the entire merged config (default/prod) as json in nconf? I know we can do JSON.stringify(nconf.get("aws:kinesis")), but how about the root node without having to know what the root node is? and also how do you override the value for "aws:kinesis" with an environment variable in this case? – Tin Ng Mar 10 '18 at 00:27
  • Perhaps you can read and process each file individually: nconf.file({file: cFile1}); console.log(nconf.get('aws:kinesis')); console.log(nconf.get('key')); nconf.file({file: cFile2}); console.log(nconf.get('db:host')); console.log(nconf.get('key')); I know this is basically like working with one nconf object, so it may not be exactly what you want. – Edwin Torres Mar 12 '18 at 13:29