dotenv-flow: Environment Variable Management

4.1.0 · active · verified Tue Apr 21

dotenv-flow is an extension of the popular `dotenv` package, designed to simplify environment variable management across multiple deployment environments in Node.js applications. It introduces robust support for `NODE_ENV`-specific `.env` files (e.g., `.env.development`, `.env.production`, `.env.test`) and `.local` overrides (e.g., `.env.development.local`) to manage environment variables dynamically based on the current `NODE_ENV`. The package automatically loads the appropriate `.env` files in a predefined order, allowing variables in more specific files to override those in more general ones. This structured approach is inspired by Ruby's `dotenv-rails` and CreateReactApp's configuration patterns, adhering closely to the Twelve-Factor App methodology's principles for storing configuration in the environment. The current stable version is 4.1.0, with an active release cadence that regularly introduces new features and bug fixes, such as customizable file patterns in v4.0.0 and explicit file lists in v4.1.0, enhancing its flexibility and control over environment loading.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize dotenv-flow and access environment variables loaded from multiple `.env` files, simulating a `development` environment with local overrides.

// For this example, assume you have these files in your project root:
// .env
//   APP_NAME=MyUniversalApp
//   GLOBAL_SETTING=true

// .env.development
//   NODE_ENV=development
//   API_URL=http://localhost:3000

// .env.development.local
//   DB_USER=dev_local
//   DEBUG_MODE=true

import dotenvFlow from 'dotenv-flow';

// It's good practice to explicitly set NODE_ENV for predictable behavior,
// though dotenv-flow also checks process.env.NODE_ENV.
// For demonstration purposes, we can override it here.
// In a real application, NODE_ENV would usually be set by the host environment.
// For example: `NODE_ENV=development node app.js` or `NODE_ENV=production node app.js`

// Simulate running in development for local testing
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

// Load environment variables based on the current NODE_ENV
const result = dotenvFlow.config();

if (result.error) {
  console.error('Failed to load environment variables:', result.error);
} else {
  console.log(`Environment loaded for NODE_ENV: ${process.env.NODE_ENV}`);
  console.log('--- Loaded Variables ---');
  console.log('APP_NAME:', process.env.APP_NAME); // From .env
  console.log('GLOBAL_SETTING:', process.env.GLOBAL_SETTING); // From .env
  console.log('API_URL:', process.env.API_URL); // From .env.development
  console.log('DB_USER:', process.env.DB_USER); // From .env.development.local
  console.log('DEBUG_MODE:', process.env.DEBUG_MODE); // From .env.development.local
  console.log('------------------------');

  // Example of using a loaded variable
  if (process.env.DEBUG_MODE === 'true') {
    console.log('Debug mode is ENABLED. Happy coding!');
  }
}

view raw JSON →