Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dotenv-expand

motdotla52.2mBSD-2-Clause12.0.1TypeScript support: included

Expand environment variables using dotenv

dotenv, expand, variables, interpolation, substitution, env, .env

readme

🎉 announcing dotenvx. expansion AND command substitution, multi-environment, encrypted envs, and more.

 

dotenv-expand NPM version

dotenv-expand

Dotenv-expand adds variable expansion on top of dotenv. If you find yourself needing to expand environment variables already existing on your machine, then dotenv-expand is your tool.

js-standard-style LICENSE codecov

Install

# Install locally (recommended)
npm install dotenv-expand --save

Or installing with yarn? yarn add dotenv-expand

Usage

Create a .env file in the root of your project:

PASSWORD="s1mpl3"
DB_PASS=$PASSWORD

As early as possible in your application, import and configure dotenv and then expand dotenv:

const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')

dotenvExpand.expand(dotenv.config())

console.log(process.env) // remove this after you've confirmed it is expanding

That's it. process.env now has the expanded keys and values you defined in your .env file.

dotenvExpand.expand(dotenv.config())

...

connectdb(process.env.DB_PASS)

Preload

Note: Consider using dotenvx instead of preloading. I am now doing (and recommending) so.

It serves the same purpose (you do not need to require and load dotenv), has built-in expansion support, adds better debugging, and works with ANY language, framework, or platform. – motdotla

You can use the --require (-r) command line option to preload dotenv & dotenv-expand. By doing this, you do not need to require and load dotenv or dotenv-expand in your application code. This is the preferred approach when using import instead of require.

$ node -r dotenv-expand/config your_script.js

The configuration options below are supported as command line arguments in the format dotenv_config_<option>=value

$ node -r dotenv-expand/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars

Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.

$ DOTENV_CONFIG_<OPTION>=value node -r dotenv-expand/config your_script.js
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv-expand/config your_script.js dotenv_config_path=/custom/path/to/.env

Examples

See tests/.env.test for simple and complex examples of variable expansion in your .env file.

Documentation

dotenv-expand exposes one function:

  • expand

Expand

expand will expand your environment variables.

const env = {
  parsed: {
    BASIC: 'basic',
    BASIC_EXPAND: '${BASIC}',
    BASIC_EXPAND_SIMPLE: '$BASIC'
  }
}

console.log(dotenvExpand.expand(env))

Options

processEnv

Default: process.env

Specify an object to write your secrets to. Defaults to process.env environment variables.

const myEnv = {}
const env = {
  processEnv: myEnv,
  parsed: {
    HELLO: 'World'
  }
}
dotenvExpand.expand(env)

console.log(myEnv.HELLO) // World
console.log(process.env.HELLO) // undefined

FAQ

What rules does the expansion engine follow?

See a full list of rules here.

How can I avoid expanding pre-existing envs (already in my process.env, for example pas$word)?

As of v12.0.0 dotenv-expand no longer expands process.env.

If you need this ability, use dotenvx by shipping an encrypted .env file with your code - allowing safe expansion at runtime.

How can I override an existing environment variable?

Use dotenvx as dotenv-expand does not support this.

dotenv-expand is a separate module (without knowledge of the loading of process.env and the .env file) and so cannot reliably know what to override.

Contributing Guide

See CONTRIBUTING.md

CHANGELOG

See CHANGELOG.md

Who's using dotenv-expand?

These npm modules depend on it.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

Unreleased

12.0.1 (2024-11-18)

Changed

  • break logic on runningParsed (#ad887)

12.0.0 (2024-11-16)

Added

  • 🎉 support alternate value expansion (see usage) (#131)

Changed

  • 🎉 Expansion logic rewritten to match dotenvx (#131)

NOTE: I recommend dotenvx over dotenv-expand when you are ready. I'm putting all my effort there for a unified standard .env implementation that works everywhere and matches bash, docker-compose, and more. In some cases it slightly improves on them - leading to more reliability for your secrets and config.

  • ⚠️ BREAKING: do NOT attempt expansion of process.env (#131)

This has always been dangerous (unexpected side effects) and is now removed. process.env should not hold values you want to expand. If for some reason you need equivalent abilities, use dotenvx. You can ship an encrypted .env file with your code - allowing safe expansion at runtime - rather than relying on trying to expand pre-existing process.env values that could for good reason have a dollar sign in them (example a password).

11.0.7 (2024-11-13)

Changed

  • 🐞 fix self-expanding undefined variable with default value (#126)

11.0.6 (2024-02-17)

Changed

  • Fix .nyc_output in .npmignore

11.0.5 (2024-02-17)

Changed

  • 🐞 fix recursive expansion when expansion key is sourced from process.env (#121)

11.0.4 (2024-02-15)

Changed

  • 🐞 fix recursive expansion when expansion keys in reverse order (#118)

11.0.3 (2024-02-11)

Changed

  • 🐞 bug fix when processEnv set to process.env rather than empty object (also test fixes which hid the bug) (#113)

11.0.2 (2024-02-10)

Changed

11.0.1 (2024-02-10)

Added

  • Added funding link in package.json

11.0.0 (2024-02-10)

Added

  • Add typings for import dotenv-expand/config (#99)
  • Support expansion of dot in env variable names like POSTGRESQL.BASE.USER (#93)
  • Add processEnv option (#105)
  • Add support for default format of ${VAR-default} (#109)

Changed

  • Do not expand prior process.env environment variables. NOTE: make sure to see updated README regarding dotenv.config({ processEnv: {} }) (#104)
  • 🐞 handle $var1$var2 (#103, #104)
  • 🐞 fix fatal recursive error when variable defines value with same variable VAR=$VAR #98

Removed

  • Remove ignoreProcessEnv option (use processEnv option going forward)

10.0.0 (2022-12-16)

Added

  • Support special characters in default expansion (#74)

9.0.0 (2022-08-30)

Added

  • Proper support for preload and cli args (#78)

8.0.3 (2022-03-21)

Changed

  • 🐞 Fixed defaults bug (#71)

8.0.2 (2022-03-11)

Changed

  • 🐞 Fixed preloading bug

8.0.1 (2022-02-03)

Added

  • Added config.js to package.json lookups

8.0.0 (2022-02-03)

Changed

  • Breaking: Bump to v16.0.0 of dotenv

Added

  • Preload support 🎉 (#31)

7.0.0 (2022-01-17)

Changed

  • Breaking: Bump to v15.0.0 of dotenv

6.0.1 (2022-01-17)

Changed

  • Updated README

6.0.0 (2022-01-17)

Changed

  • Breaking Move default export to export of expand function (#14b1f2)

Added

  • Add default expansion 🎉 (#39)
  • Add missing type descriptions

5.1.0 and prior

Please see commit history.