package
Add command substitution to dotenv
npm install dotenv-evalGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
10.1 kB
Includes dependencies.
Dotenv-eval adds command evaluation on top of dotenv. If you find yourself needing to add the output of a command in one of your environment variables, then dotenv-eval is your tool.
# Install locally (recommended)
npm install dotenv-eval --saveOr installing with yarn? yarn add dotenv-eval
Create a .env file in the root of your project:
GREETING="$(echo hello)"As early as possible in your application, import and configure dotenv and then eval dotenv:
var dotenv = require('dotenv')
var dotenvEval = require('dotenv-eval')
var myEnv = dotenv.config()
dotenvEval.eval(myEnv)
console.log(process.env)That's it. process.env now has the evaluated (command substitution) values you defined in your .env file.
DotenvEval exposes one function:
eval will evaluate (command substitution) your environment variables.
const dotenv = {
parsed: {
BASIC_SUBSTITUTE: '$(echo hello)'
}
}
const obj = dotenvEval.eval(dotenv)
console.log(obj) // { BASIC_SUBSTITUTE: 'hello' }Default: false
Turn off writing to process.env.
const dotenv = {
ignoreProcessEnv: true,
parsed: {
SHOULD_NOT_EXIST: 'testing'
}
}
const obj = dotenvEval.eval(dotenv).parsed
console.log(obj.SHOULD_NOT_EXIST) // testing
console.log(process.env.SHOULD_NOT_EXIST) // undefinedThe substitution engine roughly has the following rules:
$(command) will substitute any command inside the $( )\$(command) will escape the $(command) rather than substitute itYou can see a full list of examples here.
See CHANGELOG.md