Simple file based configuration with environment variable overrides.
This example uses toml format, but configurations can be in other file formats.
$ npm install https://github.com/thisissoon/node-simple-config
$ npm install toml
$ vi config.toml[http]
host = "0.0.0.0"
port = 4000const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.get('http.host'); // 0.0.0.0
myConfig.get('http.port'); // 4000
myConfig.has('http.port'); // trueEnvironment variables can be used to override config values from files.
Bind individual config keys to env vars
MYAPP_HTTP_PORT=5000const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.envPrefix = 'MYAPP';
config.bindEnv('http.port');
myConfig.get('http.port'); // 5000http.port is bound to the env var MYAPP_HTTP_PORT.
Automatically bind matching env vars
MYAPP_HTTP_PORT=5000
MYAPP_HTTP_HOST=127.0.0.1const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.envPrefix = 'MYAPP';
config.autoEnv();
myConfig.get('http.port'); // 5000
myConfig.get('http.host'); // 127.0.0.1The prefix and the path replacer can be configured:
AWESOME-HTTP-PORT=5000myConfig.envPrefix = 'AWESOME';
myConfig.envKeyReplacer = '-';An application should set sane default values if a config option is not provided.
const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.setDefault('http.host', '127.0.0.1');
myConfig.setDefault('http.port', 5000);
myConfig.setDefault('http.root', '/api');
myConfig.get('http.host'); // 0.0.0.0
myConfig.get('http.port'); // 4000
myConfig.get('http.root'); // /apiconst myConfig = Config.fromFile('config.json');The toml parser is not included in the dependencies as its optional, you'll need to install it if you're using toml.
$ npm install toml
const myConfig = Config.fromFile('config.toml', 'toml');