Announcement ๐ฃ
From the makers that brought you Dotenv, introducing Dotenv Sync.
Sync your .env files between machines, environments, and team members.
Join the early access list.๐
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.
# Install locally (recommended)
npm install dotenv --save
npm install dotenv-expand --save
Or installing with yarn? yarn add dotenv-expand
Usage is a cinch!
# .env file
#
# Add environment-specific variables on new lines in the form of NAME=VALUE
#
PASSWORD=s1mpl3
DB_HOST=localhost
DB_USER=root
DB_PASS=$PASSWORD
var dotenv = require('dotenv')
var dotenvExpand = require('dotenv-expand')
var myEnv = dotenv.config()
dotenvExpand.expand(myEnv)
console.log(process.env)
process.env
now has the expanded keys and values you defined in your .env
file.
See test/.env for simple and complex examples of variable expansion in your .env
file.
DotenvExpand exposes one function:
- expand
expand
will expand your environment variables.
const dotenv = {
parsed: {
BASIC: 'basic',
BASIC_EXPAND: '${BASIC}',
BASIC_EXPAND_SIMPLE: '$BASIC'
}
}
const obj = dotenvExpand.expand(dotenv)
console.log(obj)
Default: false
Turn off writing to process.env
.
const dotenv = {
ignoreProcessEnv: true,
parsed: {
SHOULD_NOT_EXIST: 'testing'
}
}
const obj = dotenvExpand.expand(dotenv).parsed
console.log(obj.SHOULD_NOT_EXIST) // testing
console.log(process.env.SHOULD_NOT_EXIST) // undefined
The expansion engine roughly has the following rules:
$KEY
will expand any env with the nameKEY
${KEY}
will expand any env with the nameKEY
\$KEY
will escape the$KEY
rather than expand${KEY:-default}
will first attempt to expand any env with the nameKEY
. If not one, then it will returndefault
You can see a full list of examples here.
See CONTRIBUTING.md
See CHANGELOG.md