Dotenv Project
Dotenv Project
<img src="https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.png"
alt="dotenv" align="right" />
[](https://travis-ci.org/motdotla/dotenv)
[![Build status]
(https://ci.appveyor.com/api/projects/status/github/motdotla/dotenv?svg=true)]
(https://ci.appveyor.com/project/motdotla/dotenv/branch/master)
[]
(https://www.npmjs.com/package/dotenv)
[](https://github.com/feross/standard)
[](https://coveralls.io/github/motdotla/dotenv?branch=coverall-
intergration)
## Install
```bash
# with npm
npm install dotenv
# or with Yarn
yarn add dotenv
```
## Usage
```javascript
require('dotenv').config()
```
```dosini
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
```
That's it.
`process.env` now has the keys and values you defined in your `.env` file.
```javascript
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
```
### Preload
```bash
$ node -r dotenv/config your_script.js
```
The configuration options below are supported as command line arguments in the
format `dotenv_config_<option>=value`
```bash
$ node -r dotenv/config your_script.js
dotenv_config_path=/custom/path/to/your/env/vars
```
```bash
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
```
```bash
$ DOTENV_CONFIG_ENCODING=base64 node -r dotenv/config your_script.js
dotenv_config_path=/custom/path/to/.env
```
## Config
_Alias: `load`_
`config` will read your .env file, parse the contents, assign it to
[`process.env`]
(https://nodejs.org/docs/latest/api/process.html#process_process_env),
and return an Object with a `parsed` key containing the loaded content or an
`error` key if it failed.
```js
const result = dotenv.config()
if (result.error) {
throw result.error
}
console.log(result.parsed)
```
#### Path
You may specify a custom path if your file containing environment variables is
located elsewhere.
```js
require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })
```
#### Encoding
Default: `utf8`
You may specify the encoding of your file containing environment variables.
```js
require('dotenv').config({ encoding: 'base64' })
```
#### Debug
Default: `false`
You may turn on logging to help debug why certain keys or values are not being set
as you expect.
```js
require('dotenv').config({ debug: process.env.DEBUG })
```
## Parse
The engine which parses the contents of your file containing environment
variables is available to use. It accepts a String or Buffer and will return
an Object with the parsed keys and values.
```js
const dotenv = require('dotenv')
const buf = Buffer.from('BASIC=basic')
const config = dotenv.parse(buf) // will return an object
console.log(typeof config, config) // object { BASIC : 'basic' }
```
### Options
#### Debug
Default: `false`
You may turn on logging to help debug why certain keys or values are not being set
as you expect.
```js
const dotenv = require('dotenv')
const buf = Buffer.from('hello world')
const opt = { debug: true }
const config = dotenv.parse(buf, opt)
// expect a debug message because the buffer is not in KEY=VAL form
```
### Rules
```
{MULTILINE: 'new
line'}
```
## FAQ
> In a twelve-factor app, env vars are granular controls, each fully orthogonal to
other env vars. They are never grouped together as “environments”, but instead are
independently managed for each deploy. This is a model that scales up smoothly as
the app naturally expands into more deploys over its lifetime.
>
> – [The Twelve-Factor App](http://12factor.net/config)
We will never modify any environment variables that have already been set. In
particular, if there is a variable in your `.env` file which collides with one that
already exists in your environment, then that variable will be skipped. This
behavior allows you to override all `.env` configurations with a machine-specific
environment, although it is not recommended.
If you want to override `process.env` you can do something like this:
```javascript
const fs = require('fs')
const dotenv = require('dotenv')
const envConfig = dotenv.parse(fs.readFileSync('.env.override'))
for (let k in envConfig) {
process.env[k] = envConfig[k]
}
```
```js
const dotenv = require('dotenv')
const variableExpansion = require('dotenv-expand')
const myEnv = dotenv.config()
variableExpansion(myEnv)
```
Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
ES2015 and beyond offers modules that allow you to `export` any top-level
`function`, `class`, `var`, `let`, or `const`.
> When you run a module containing an `import` declaration, the modules it imports
are loaded first, then each module body is executed in a depth-first traversal of
the dependency graph, avoiding cycles by skipping anything already executed.
>
> – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-
modules/)
You must run `dotenv.config()` before referencing any environment variables. Here's
an example of problematic code:
`errorReporter.js`:
```js
import { Client } from 'best-error-reporting-service'
`index.js`:
```js
import dotenv from 'dotenv'
import errorReporter from './errorReporter'
dotenv.config()
errorReporter.client.report(new Error('faq example'))
```
1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need
to `import` dotenv with this approach_)
2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call
`dotenv.config()` and must pass options via the command line with this approach_)
3. Create a separate file that will execute `config` first as outlined in [this
comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-
255298822)
## Contributing Guide
See [CONTRIBUTING.md](CONTRIBUTING.md)
## Change Log
See [CHANGELOG.md](CHANGELOG.md)
## License
See [LICENSE](LICENSE)
* [jaws](https://github.com/jaws-framework/jaws-core-js)
* [node-lambda](https://github.com/motdotla/node-lambda)
* [resume-cli](https://www.npmjs.com/package/resume-cli)
* [phant](https://www.npmjs.com/package/phant)
* [adafruit-io-node](https://github.com/adafruit/adafruit-io-node)
* [mockbin](https://www.npmjs.com/package/mockbin)
* [and many more...](https://www.npmjs.com/browse/depended/dotenv)
* [require-environment-variables](https://github.com/bjoshuanoah/require-
environment-variables)
* [dotenv-safe](https://github.com/rolodato/dotenv-safe)
* [envalid](https://github.com/af/envalid)
* [lookenv](https://github.com/RodrigoEspinosa/lookenv)
* [run.env](https://www.npmjs.com/package/run.env)
* [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack)
* [env-path](https://github.com/benrei/env-path)