Socket
Socket
Sign inDemoInstall

dotenv-expand

Package Overview
Dependencies
1
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dotenv-expand

Expand environment variables using dotenv


Version published
Weekly downloads
15M
increased by0.82%
Maintainers
1
Install size
95.0 kB
Created
Weekly downloads
 

Package description

What is dotenv-expand?

The dotenv-expand npm package is an extension for dotenv. It allows you to have more complex .env files by enabling variable expansion within your environment variables. This means you can reference other environment variables within your .env file, which dotenv by itself does not support.

What are dotenv-expand's main functionalities?

Variable Expansion

This feature allows you to reference other variables in your .env file. For example, if you have a BASE_URL variable, you can use it to construct the API_URL variable.

require('dotenv').config();
require('dotenv-expand')(process.env);

// .env file
// BASE_URL=https://myapi.com
// API_URL=${BASE_URL}/v1

Nested Variable Expansion

This feature allows for nested variable expansion where you can use multiple environment variables to construct a new one.

require('dotenv').config();
require('dotenv-expand')(process.env);

// .env file
// URL=https://myapi.com
// VERSION=v1
// API_URL=${URL}/${VERSION}

Other packages similar to dotenv-expand

Changelog

Source

11.0.6 (2024-02-17)

Changed

  • Fix .nyc_output in .npmignore

Readme

Source
🎉 announcing dotenvx. run anywhere, multi-environment, encrypted envs.

 

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

Keywords

FAQs

Last updated on 18 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc