Simple test to illustrate how
config.jsworks with regards to defaulting and overriding.
- Config layout
- ./config/default.json
- ./config/development.json
- ./config/test.json
- ./config/production.json
- Outcome
It appears that it works simaliar to Object.assign({},{})
e.g.
const fs = require('fs')
const path = require('path')
const config = Object.assign(
JSON.parse(fs.readFileSync(path.join(__dirname,'config','default.json'),'utf-8')),
JSON.parse(fs.readFileSync(path.join(__dirname,'config',`${process.env.NODE_ENV}.json`),'utf-8')))./config
├── default.json
├── development.json
├── production.json
└── test.json
{
"overrideable": "default.json",
"defaultValue": "default.json"
}{
"overrideable": "development.json"
}{
"overrideable": "test.json"
}{
"overrideable": "production.json"
}const config = require('config')
console.log(
'NODE_ENV=',
process.env.NODE_ENV
);
console.log(
'config.overrideable = ',
config.has('overrideable') ? config.get('overrideable') : 'undefined'
);
console.log(
'config.defaultValue = ',
config.has('defaultValue') ? config.get('defaultValue') : 'undefined'
);export NODE_ENV= && node index.js
NODE_ENV=
config.overrideable = development.json
config.defaultValue = default.json
export NODE_ENV=development && node index.js
NODE_ENV= development
config.overrideable = development.json
config.defaultValue = default.json
export NODE_ENV=test && node index.js
NODE_ENV= test
config.overrideable = test.json
config.defaultValue = default.json
export NODE_ENV=production && node index.js
NODE_ENV= production
config.overrideable = production.json
config.defaultValue = default.json