https://github.com/motdotla/dotenv-eval
Add command substitution to dotenv
https://github.com/motdotla/dotenv-eval
Last synced: 28 days ago
JSON representation
Add command substitution to dotenv
- Host: GitHub
- URL: https://github.com/motdotla/dotenv-eval
- Owner: motdotla
- Created: 2022-02-09T21:29:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-05-24T00:39:01.000Z (over 3 years ago)
- Last Synced: 2025-07-24T11:39:55.364Z (3 months ago)
- Language: JavaScript
- Size: 430 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
[](https://www.dotenv.org/r/github.com/dotenv-org/dotenv-vault?r=1)
# dotenv-eval
Dotenv-eval adds command evaluation on top of
[dotenv](http://github.com/motdotla/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
```bash
# Install locally (recommended)
npm install dotenv-eval --save
```Or installing with yarn? `yarn add dotenv-eval`
## Usage
Create a `.env` file in the root of your project:
```dosini
GREETING="$(echo hello)"
```As early as possible in your application, import and configure dotenv and then eval dotenv:
```javascript
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.
## Documentation
DotenvEval exposes one function:
* eval
### Eval
`eval` will evaluate (command substitution) your environment variables.
```js
const dotenv = {
parsed: {
BASIC_SUBSTITUTE: '$(echo hello)'
}
}const obj = dotenvEval.eval(dotenv)
console.log(obj) // { BASIC_SUBSTITUTE: 'hello' }
```#### Options
##### ignoreProcessEnv
Default: `false`
Turn off writing to `process.env`.
```js
const dotenv = {
ignoreProcessEnv: true,
parsed: {
SHOULD_NOT_EXIST: 'testing'
}
}
const obj = dotenvEval.eval(dotenv).parsedconsole.log(obj.SHOULD_NOT_EXIST) // testing
console.log(process.env.SHOULD_NOT_EXIST) // undefined
```## FAQ
### What rules does the command substitution engine follow?
The 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](https://github.com/motdotla/dotenv-eval/blob/master/tests/.env).
## Contributing Guide
See [CONTRIBUTING.md](CONTRIBUTING.md)
## CHANGELOG
See [CHANGELOG.md](CHANGELOG.md)