What is this?
This package helps you setup hierarchical configuration
for your node.js
applications. It enables you to have different configuration for different environments i.e. for local, development, staging, production etc. As it's name suggests, it's very tiny with no external dependencies and under 100 lines of code.
How does it work?
It tries to follow a very simple and intuitive convention so that you don't have to do too much work.
-
It tries to figure out the directory where you might be keeping the config files. By default it searches for
config
directory in the root of your project, if that is not the place you want to keep config files then you can setNODE_CONFIG_PATH
environment variable, which should be the path of your config directory relative to the root of your project. -
All the files in the config directory should be JSON file with
.json
extension. -
Sometimes the only reason you want to have a configuration system is to externalize values from your code, in this case all you need is to create a file named
default.json
in the config directory (as mentioned in #1) and put all the config settings in that file. -
In cases where you want to override some of the config settings present in
default.json
or want to add some more settings based on the deployment environment, you need to create additional files in the config directory and set the name of the file toNODE_ENV
environment variable. For example if your deployment environment isstaging
then createstaging.json
under config directory. -
You can override settings using env variable, for example if you have
process.env.connectionString
set, then it takes precedence and will be the effective value, overriding the value set in the config files.
Example Setup
If you have following files in you configuration directory (i.e. either config
or whatever path you have set in NODE_CONFIG_PATH
):
├── default.json├── development.json├── local.json├── production.json└── staging.json
and you have NODE_ENV
env variale set to staging
then all the settings present in the default.json
will be taken first and then all the settings present in the staging.json
would be applied on top of it, in which case additional settings will be added, settings with the same key
would be overwritten and missing setting would be carried from the default.json
.
Quick Start
Add the dependency using npm
:
npm install tiny-config --save
mkdir config
considering you have following default.json
and production.json
is:
considering you have set NODE_ENV
to production
, you can access the setting as follows:
var config = ; var connectionString = config;//"prod_connection_string" var isStackTraceEnabled = config;//truevar retryConfig = config;//{"maxAttempts": 5} var retryAttempts = config;//5 //considering you have set `version` env var to "2.0.0" var version = config//2.0.0 var all = config//Will return the map which has effective config settings