dynamic-config
Dynamic configuration files
Loads configuration files depending on:
- argv:
node app.js --env production
- env:
export NODE_ENV=production; node app.js
Expects a .js
file as config so you can add dynamic content.
Installation
npm install dynamic-config --save
Options
defaultEnv: string = "develop"
Define which env should be set as default.
log: boolean = false
Enable logging of path/env resolution.
envName: string = "env"
The argument/env variable name we expect.
Example
// config/index.js const DynamicConfig = ;const dynamicConfig = defaultEnv: "develop" log: true; moduleexports = dynamicConfig;
// config/develop/config.js moduleexports = whereami: "develop"
// app.jsconst config = ; console;
node appjs whereami: 'develop' // Set environment via argsnode appjs --env prod whereami: 'prod' // Set environment via env; node appjs whereami: 'stage'
Plugins
extend
These plugins allow you to override specific config fields by applying them via env, argv or a separate local config file.
const dynamicConfig = new ; // extend from envdynamicConfig; // extend from filedynamicConfig; // extend from argvdynamicConfig; moduleexports = dynamicConfig;
Hint: The order in which the plugins are applied is important. In the above code snippet, config fields defined via arguments would override their counterparts from an override file, which itself overrides fields from environment variables. This is probably a suitable order for most projects.
node appjs name: 'superApp' port: 9000 // Overwrite via argvnode appjs --port 80// ... or ...node appjs --port=80 name: 'superApp' port: 80 // Overwrite via env name: 'superApp' port: 90 // Order matters...; node appjs --port 80 name: 'superApp' port: 80
Extend via file
Create a file named the same as your config, but contains .local
in front of the extension, like config.js
becomes config.local.js
.
In the config extension file you can define any subset of the object, that is defined in the main config and it would overwrite the corresponding value. Both configs are merged via deep-assign.
// config.jsmoduleexports = a: 1 b: c: "c" d: 2 e: 3 // config.local.jsmoduleexports = e: "e" b: d: "d" // result a: 1 b: c: "c" d: "d" e: "e"