json-evn-configurator
v1.0.6
Published
Creates object with fields that are merged from environmental variables and fields from json file
Downloads
3,549
Readme
json-evn-configurator
Creates JS object with fields that are merged from environmental variables and fields from json file
Usage example
- Create config.json file with some configuration fields:
{
"CONFIGURATION_FIELD_1": "some config data",
"CONFIGURATION_FIELD_2": "another config data"
}
- Pass the path to the config.json file to the json-env-configurator:
const configurator = require(`json-env-configurator`);
const config = configurator(pathToJsonFile);
console.log(config.CONFIGURATION_FIELD_1); // -> some config data
console.log(config.CONFIGURATION_FIELD_2); // -> another config data
If you specify environmental variable with the same name as in config.json file, environmental variable will has higher priority:
CONFIGURATION_FIELD_2=overridden node app.js
app.js:
const configurator = require(`json-env-configurator`);
const config = configurator(pathToJsonFile);
console.log(config.CONFIGURATION_FIELD_1); // -> some config data
console.log(config.CONFIGURATION_FIELD_2); // -> overridden
- You can specify special prefix for environmental variables to separate configuration by environmental variables with namespaces
Let's create another configuration file - config2.json:
{
"HOST": "localhost",
"PORT": "3000"
}
FIRST_CONFIG.CONFIGURATION_FIELD_2=overridden SECOND_CONFIG.PORT=8080 node app.js
app.js:
const configurator = require(`json-env-configurator`);
const firstConfig = configurator(pathToFirstJsonFile, `FIRST_CONFIG`);
const secondConfig = configurator(pathToSecondJsonFile, `SECOND_CONFIG`);
console.log(firstConfig.CONFIGURATION_FIELD_1); // -> some config data
console.log(firstConfig.CONFIGURATION_FIELD_2); // -> overridden
console.log(secondConfig.HOST); // -> localhost
console.log(secondConfig.PORT); // -> 8080