config vs dotenv vs dotenv-expand vs dotenv-flow
Node.js 环境配置管理库
configdotenvdotenv-expanddotenv-flow类似的npm包:

Node.js 环境配置管理库

在 Node.js 开发中,环境配置管理是一个重要的方面,涉及到如何安全和高效地管理应用程序的配置参数。这些库提供了不同的功能来处理环境变量和配置文件,使得开发者能够灵活地管理不同环境(如开发、测试和生产)的配置。使用这些库可以提高代码的可维护性和可移植性,确保应用程序在不同环境中能够正常运行。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
config1,237,5236,425207 kB1323 天前MIT
dotenv020,33493.3 kB621 个月前BSD-2-Clause
dotenv-expand01,04919.5 kB37 个月前BSD-2-Clause
dotenv-flow090060.3 kB82 年前MIT

功能对比: config vs dotenv vs dotenv-expand vs dotenv-flow

功能复杂性

  • config:

    config 提供了一个全面的配置管理解决方案,支持多种文件格式和环境,适合复杂的应用程序。它允许开发者定义默认配置,并根据环境自动加载相应的配置文件。

  • dotenv:

    dotenv 是一个简单的库,专注于从 .env 文件加载环境变量,适合小型项目或简单的应用程序。

  • dotenv-expand:

    dotenv-expand 在 dotenv 的基础上增加了对变量扩展的支持,使得可以在 .env 文件中引用其他环境变量,适合需要动态构建环境变量的场景。

  • dotenv-flow:

    dotenv-flow 支持多环境配置,允许在不同环境中使用不同的 .env 文件,适合需要管理多个环境配置的复杂应用程序。

易用性

  • config:

    config 的学习曲线相对较陡,因为它提供了丰富的功能和灵活性,适合需要复杂配置管理的开发者。

  • dotenv:

    dotenv 的使用非常简单,只需安装并调用一个方法即可加载环境变量,适合初学者和小型项目。

  • dotenv-expand:

    dotenv-expand 的使用相对简单,但需要与 dotenv 配合使用,适合需要扩展功能的开发者。

  • dotenv-flow:

    dotenv-flow 的使用也很简单,提供了易于理解的 API,适合需要多环境支持的开发者。

环境支持

  • config:

    config 支持多种环境配置,能够根据 NODE_ENV 自动加载不同的配置文件,适合大型应用程序。

  • dotenv:

    dotenv 主要用于加载单一环境的配置,适合简单应用程序。

  • dotenv-expand:

    dotenv-expand 主要用于扩展 dotenv 的功能,适合需要引用其他环境变量的场景。

  • dotenv-flow:

    dotenv-flow 支持多环境配置,能够根据不同的环境加载相应的 .env 文件,适合复杂应用程序。

维护性

  • config:

    config 提供了结构化的配置管理,便于维护和扩展,适合长期项目。

  • dotenv:

    dotenv 的维护性较高,但由于功能简单,可能不适合复杂项目。

  • dotenv-expand:

    dotenv-expand 的维护性较高,适合需要动态构建环境变量的项目。

  • dotenv-flow:

    dotenv-flow 的维护性较高,适合需要管理多个环境配置的复杂应用程序。

社区支持

  • config:

    config 拥有活跃的社区支持,提供了丰富的文档和示例,适合需要社区支持的开发者。

  • dotenv:

    dotenv 是一个广泛使用的库,拥有良好的社区支持和文档,适合初学者。

  • dotenv-expand:

    dotenv-expand 的社区支持相对较小,但仍然有一定的使用基础,适合需要扩展功能的开发者。

  • dotenv-flow:

    dotenv-flow 也有一定的社区支持,适合需要多环境支持的开发者。

如何选择: config vs dotenv vs dotenv-expand vs dotenv-flow

  • config:

    选择 config 如果你需要一个强大的配置管理解决方案,支持多种文件格式(如 JSON、YAML、JavaScript),并且能够根据环境自动加载不同的配置文件。它适合大型应用程序,提供了更复杂的配置管理功能。

  • dotenv:

    选择 dotenv 如果你只需要简单地从 .env 文件加载环境变量到 process.env 中。它非常轻量,易于使用,适合小型项目或简单的应用程序。

  • dotenv-expand:

    选择 dotenv-expand 如果你需要在 dotenv 的基础上扩展变量的功能,允许在 .env 文件中引用其他环境变量。这对于需要动态构建环境变量的应用程序非常有用。

  • dotenv-flow:

    选择 dotenv-flow 如果你需要支持多环境的配置管理,允许在不同的环境中使用不同的 .env 文件,并且可以处理环境变量的优先级。这对于复杂的应用程序非常有用,能够更好地管理不同环境的配置。

config的README

Configure your Node.js Applications

npm package Downloads Issues

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.

TypeScript

Type declarations are published under types/ and resolved via typesVersions. Subpath typings are included for config/async, config/defer, config/parser, config/raw, and config/lib/util in addition to the main config entrypoint.

Articles

Further Information

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

Contributors

lorenwestjdmarshallmarkstosi­Moseselliotttfmdkitzman
jfelegeleachi­M2kjosxenyoleosuncinarthanzel
leonardovillelajeremy-daley-krsimon-scherzingerBadger­Badger­Badger­Badgernsaboviccunneen
Osterjourth507tiny-rac00neheikesfgheorgheroncli
superovenairdrummingfoolwmertensXadilla­Xinsidedsbert

License

May be freely distributed under the MIT license.

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