dotenv-flow

Loads environment variables from .env.[development|test|production][.local]...

README

dotenv-flow


dotenv-flow

_dotenv is a zero-dependency npm module that loads environment variables from a.env file into [process.env](https://nodejs.org/docs/latest/api/process.html#process_process_env)._


dotenv-flow extends _dotenv_, adding support of NODE_ENV-specific `.env files _like .env.development, .env.test, .env.stage, and .env.production,_ and the appropriate .env.local` overrides.

It allows your app to have multiple environments _(like "development", "test", "stage", and "production" respectively)_ with selectively-adjusted environment variable setups and load them dynamically depending on the current NODE_ENV.

In addition to that, .env*.local overrides add the ability to overwrite variables locally for development, testing, and debugging purposes
_(note that the appropriate .env*.local entry should be added to your .gitignore)_.

🌱 Inspired by _Ruby's dotenv (a.k.a. dotenv-rails) gem_, _CreateReactApp's *storing configs in `.env` files approach_,
the _Twelve-Factor App methodology_ in general, and _its store config in the environment section_ in particular.

Installation


Using NPM:

  1. ```sh
  2. $ npm install dotenv-flow --save
  3. ```

Using Yarn:

  1. ```sh
  2. $ yarn add dotenv-flow
  3. ```

Using PNPM:

  1. ```sh
  2. $ pnpm add dotenv-flow
  3. ```


Usage


As early as possible in your Node.js app, initialize dotenv-flow:

  1. ```js
  2. require('dotenv-flow').config();
  3. ```

It will allow you to configure and use dotenv-flow from your code programmatically.

If you're using TypeScript or ES Modules:

  1. ```ts
  2. import dotenvFlow from 'dotenv-flow';
  3. dotenvFlow.config();
  4. ```

Alternatively, you can use the default config entry point that allows you to configure dotenv-flow using command switch flags or predefined environment variables:

  1. ```js
  2. require('dotenv-flow/config');
  3. ```

Or even make dotenv-flow load environment variables for your app without adding it to the code using preload technique:

  1. ```sh
  2. $ node -r "dotenv-flow/config" your_app.js
  3. ```

It works with ts-node as well:

  1. ```sh
  2. $ ts-node -r "dotenv-flow/config" your_app.ts
  3. ```

How it works

Once dotenv-flow is initialized (using .config or any other method above), environment variables defined in your `.env files are loaded and become accessible in your Node.js app via process.env.`.

For example, let's suppose that you have the following .env* files in your project:

  1. ```sh
  2. # .env

  3. DATABASE_HOST=127.0.0.1
  4. DATABASE_PORT=27017
  5. DATABASE_USER=default
  6. DATABASE_PASS=
  7. DATABASE_NAME=my_app
  8. ```

  1. ```sh
  2. # .env.local

  3. DATABASE_USER=local-user
  4. DATABASE_PASS=super-secret
  5. ```

  1. ```sh
  2. # .env.development

  3. DATABASE_NAME=my_app_dev
  4. ```

  1. ```sh
  2. # .env.test

  3. DATABASE_NAME=my_app_test
  4. ```

  1. ```sh
  2. # .env.production

  3. DATABASE_NAME=my_app_prod
  4. ```

  1. ```sh
  2. # .env.production.local

  3. DATABASE_HOST=10.0.0.32
  4. DATABASE_PORT=27017
  5. DATABASE_USER=devops
  6. DATABASE_PASS=1qa2ws3ed4rf5tg6yh
  7. DATABASE_NAME=application_storage
  8. ```

  1. ```js
  2. // your_script.js

  3. require('dotenv-flow').config();

  4. console.log('database host:', process.env.DATABASE_HOST);
  5. console.log('database port:', process.env.DATABASE_PORT);
  6. console.log('database user:', process.env.DATABASE_USER);
  7. console.log('database pass:', process.env.DATABASE_PASS);
  8. console.log('database name:', process.env.DATABASE_NAME);
  9. ```

And if you run your_script.js in the development environment, like:

  1. ```sh
  2. $ NODE_ENV=development node your_script.js
  3. ```

you'll get the following output:

  1. ```text
  2. database host: 127.0.0.1
  3. database port: 27017
  4. database user: local-user
  5. database pass: super-secret
  6. database name: my_app_dev
  7. ```

Or if you run the same script in the production environment:

  1. ```sh
  2. $ NODE_ENV=production node your_script.js
  3. ```

you'll get the following:

  1. ```text
  2. database host: 10.0.0.32
  3. database port: 27017
  4. database user: devops
  5. database pass: 1qa2ws3ed4rf5tg6yh
  6. database name: application_storage
  7. ```

Note that the .env*.local files should be ignored by your version control system (refer the Files under version control section below to learn more), and you should have the .env.production.local file only on your production deployment machine.