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.
npm install dotenv-flowVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize dotenv-flow and access environment variables loaded from multiple `.env` files, simulating a `development` environment with local overrides.
Add `/.env.local` and `/.env.*.local` to your project's `.gitignore` file.
Review the documentation on file precedence and carefully structure your `.env` files. Use `options.debug: true` (since v4.0.0) to see which files are being loaded.
Always explicitly set `NODE_ENV` in your deployment environment (e.g., `NODE_ENV=production node app.js`) or within your application startup script before calling `dotenvFlow.config()`.
Place `require('dotenv-flow').config();` or `import dotenvFlow from 'dotenv-flow'; dotenvFlow.config();` at the very top of your main application file (e.g., `index.js` or `app.ts`).Ensure your `.env` files are in the project root or provide the correct `path` option to `dotenvFlow.config({ path: '/absolute/path/to/project/root' })`.For ESM, use `import dotenvFlow from 'dotenv-flow';` for programmatic use or `import 'dotenv-flow/config';` for preloading.
Use `import dotenvFlow from 'dotenv-flow';` and then call `dotenvFlow.config();`. The `config` function is a method of the default `dotenvFlow` object, not a named export.
Verify that `process.env.NODE_ENV` is correctly set for your desired environment. Check the order of your `.env` files for unintended overrides. Use `dotenvFlow.config({ debug: true })` (since v4.0.0) to output detailed loading information.No dependency data recorded yet.