node-config

Node.js Application Configuration

README

Configure your Node.js Applications
===================================
NPM   Build Status  

Introduction

Node-config organizes hierarchical configurations for your app deployments.

It lets you define a set of default parameters,
and extend them for different deployment environments (development, qa,
staging, production, etc.).

Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables,

This gives your application a consistent configuration interface shared among a
growing list of npm modules also using node-config.

Project Guidelines

Simple - Get started fast
Powerful - For multi-node enterprise deployment
Flexible - Supporting multiple config file formats
Lightweight - Small file and memory footprint
Predictable - Well tested foundation for module and app developers

Quick Start
The following examples are in JSON format, but configurations can be in other file formats.

Install in your app directory, and edit the default config file.

  1. ``` sh
  2. $ npm install config
  3. $ mkdir config
  4. $ vi config/default.json
  5. ```
  1. ``` js
  2. {
  3.   // Customer module configs
  4.   "Customer": {
  5.     "dbConfig": {
  6.       "host": "localhost",
  7.       "port": 5984,
  8.       "dbName": "customers"
  9.     },
  10.     "credit": {
  11.       "initialLimit": 100,
  12.       // Set low for development
  13.       "initialDays": 1
  14.     }
  15.   }
  16. }
  17. ```

Edit config overrides for production deployment:

  1. ``` sh
  2. $ vi config/production.json
  3. ```

  1. ``` json
  2. {
  3.   "Customer": {
  4.     "dbConfig": {
  5.       "host": "prod-db-server"
  6.     },
  7.     "credit": {
  8.       "initialDays": 30
  9.     }
  10.   }
  11. }
  12. ```

Use configs in your code:

  1. ``` js
  2. const config = require('config');
  3. //...
  4. const dbConfig = config.get('Customer.dbConfig');
  5. db.connect(dbConfig, ...);

  6. if (config.has('optionalFeature.detail')) {
  7.   const detail = config.get('optionalFeature.detail');
  8.   //...
  9. }
  10. ```

config.get() will throw an exception for undefined keys to help catch typos and missing values.
Use config.has() to test if a configuration value is defined.

Start your app server:

  1. ``` sh
  2. $ export NODE_ENV=production
  3. $ node my-app.js
  4. ```

Running in this configuration, the port and dbName elements of dbConfig
will come from the default.json file, and the host element will
come from the production.json override file.

Articles


Further Information
If you still don't see what you are looking for, here are some more resources to check:

The wiki may have more pages which are not directly linked from here.
Review questions tagged with node-config on StackExchange. These are monitored bynode-config contributors.
Search the issue tracker. Hundreds of issues have already been discussed and resolved there.

Contributors
lorenwestmarkstosiMoseselliotttfjfelegeleachiM2k
josxenyoleosuncinarthanzeleheikesth507
OsterjourcunneennsabovicBadgerBadgerBadgerBadgersimon-scherzingerleonardovillela
axelhzfbenkroegerfgheorgheIvanVergilievjpwilliamsjaylynch
jberrischkgoerlitzbertho-zeroNguyenMatthieunitzan-shakedrobertrossmann

License

May be freely distributed under the MIT license.

Copyright (c) 2010-2022 Loren West