node-config

Node.js Application Configuration

Github星跟踪图

Configure your Node.js Applications

NPM  
Build Status  
release notes

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,
command line parameters, or external sources.

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.

$ npm install config
$ mkdir config
$ vi config/default.json
{
  // Customer module configs
  "Customer": {
    "dbConfig": {
      "host": "localhost",
      "port": 5984,
      "dbName": "customers"
    },
    "credit": {
      "initialLimit": 100,
      // Set low for development
      "initialDays": 1
    }
  }
}

Edit config overrides for production deployment:

 $ vi config/production.json
{
  "Customer": {
    "dbConfig": {
      "host": "prod-db-server"
    },
    "credit": {
      "initialDays": 30
    }
  }
}

Use configs in your code:

const config = require('config');
//...
const dbConfig = config.get('Customer.dbConfig');
db.connect(dbConfig, ...);

if (config.has('optionalFeature.detail')) {
  const detail = config.get('optionalFeature.detail');
  //...
}

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:

$ export NODE_ENV=production
$ node my-app.js

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:

Contributors

License

May be freely distributed under the MIT license.

Copyright (c) 2010-2018 Loren West
and other contributors

主要指标

概览
名称与所有者node-config/node-config
主编程语言JavaScript
编程语言JavaScript (语言数: 4)
平台
许可证Other
所有者活动
创建于2010-12-02 00:33:11
推送于2025-08-15 21:36:17
最后一次提交2025-08-15 14:33:12
发布数13
最新版本名称v4.2.0 (发布于 )
第一版名称v0.4.19 (发布于 )
用户参与
星数6.4k
关注者数80
派生数503
提交数782
已启用问题?
问题数546
打开的问题数14
拉请求数185
打开的拉请求数4
关闭的拉请求数96
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?