11.0.6 ā€¢ Published 2 months ago

dotenv-expand v11.0.6

Weekly downloads
5,470,600
License
BSD-2-Clause
Repository
github
Last release
2 months ago

Ā 

dotenv-expand NPM version

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(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(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?

The expansion engine roughly has the following rules:

  • $KEY will expand any env with the name KEY
  • ${KEY} will expand any env with the name KEY
  • \$KEY will escape the $KEY rather than expand
  • ${KEY:-default} will first attempt to expand any env with the name KEY. If not one, then it will return default
  • ${KEY-default} will first attempt to expand any env with the name KEY. If not one, then it will return default

You can see a full list of rules here.

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

Modify your dotenv.config to write to an empty object and pass that to dotenvExpand.processEnv.

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

const myEnv = dotenv.config({ processEnv: {} }) // prevent writing to `process.env`

dotenvExpand.expand(myEnv)

Contributing Guide

See CONTRIBUTING.md

CHANGELOG

See CHANGELOG.md

Who's using dotenv-expand?

These npm modules depend on it.

react-scriptslazy-universal-dotenv@xata.io/cliyoyuo-scriptsxmi-scriptszhouhaifei-react-scriptszikix-core@bsolution/dev@bsolution/fast@bsolution/scripts@mdf.js/provider@proteria/react-scripts@obstinate/react-script@obstinate/vue-script@zhouhaifei/test@uukit/react-dev-kit@uukit/scripts@savaleukhin/react-scripts@scentregroup/react-scripts@procore/core-scripts@talesman/react-scriptsblitzcarrier_anal@gd-uikit/react-scriptscbbpa-clientcc-gram-demo@commercetools-frontend/mc-scripts@architect-io/clireact-wind-ui@ishopee/cli-servicesmart-input-demovuedragdropuploadimagesbyteplayerkstick-react-scripts@boris47/electron-webpackwatsize-test-react-scripts@psimk/react-scriptsglobal-martcomatch-mvp-debugging@darapti/darapti-react-scriptsswitch_mostqroda-sdkqroda-sdk-v1metaliquid-react-scriptsrockywu-ui@tomas2d/react-scriptssaltarin-ui-storybookjbojcic-react-scripts@enginite/electron-starter@paperlesspost/react-scripts@jvhellemondt/custom-react-scripts@st4rfox/electron-scriptssv-component-librarysv-component-library-1sv-component-library-2@thuanpq/airvert-ui@xinyifly/react-scripts@ipavinthan/react-scriptsmio-cli-service@katalabut/react-scriptsandculturecode-react-scriptsb1x-cor-poc-apiwebeetle-react-scriptsyadong-react@agney/react-scripts-tailwindzackmoreplay-react-scriptsgooapps-react-opsthefarm-react-scripts@wywk/headquaters-marketing-eventconcord-ui-plugin-react-scriptssebrae-boilerplatesebrae-react-scripts@cloudhuset/zd-app-react-scriptsidelic-react-scriptstest-app-6test-app-7tm-components-test@parcel-nightly-unofficial/core@jvhellemondt/react-scriptsarchitect-cliearthling-interactive-react-scriptshoogo-niuyapingreact-epic-image-slidermylicensepejann-scriptsmy-custom-react-jwplayerinversify-react-scriptselectron-cra-scriptsecom-catalogkolbeinsson88-assignment5kolbeinsson88-stylesjs-advancedplatinion-paris-react-componentsreact-scripts-public-urlzula-bravosmartinput-reactstar-react-scripts@amajola/react-scriptsactive-react-scripts@priyanknamdeo16/react-scripts
11.0.6

2 months ago

11.0.5

2 months ago

11.0.4

2 months ago

11.0.3

2 months ago

11.0.2

2 months ago

11.0.0

2 months ago

11.0.1

2 months ago

10.0.0

1 year ago

9.0.0

2 years ago

8.0.3

2 years ago

8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

7.0.0

2 years ago

5.1.0

5 years ago

5.0.0

5 years ago

4.2.0

6 years ago

4.1.0

6 years ago

4.0.1

7 years ago

4.0.0

7 years ago

0.1.0

8 years ago