├── examples ├── es6 │ ├── .env │ ├── run_me │ └── index.js ├── basic │ ├── .env │ └── index.js ├── preload │ ├── .env │ ├── run_me │ └── index.js └── es6-preload │ ├── .env │ ├── run_me │ └── index.js ├── dotenv.png ├── .travis.yml ├── .npmignore ├── .editorconfig ├── config.js ├── .gitignore ├── test ├── .env ├── config.js └── main.js ├── CONTRIBUTING.md ├── package.json ├── LICENSE ├── lib └── main.js ├── CHANGELOG.md └── README.md /examples/es6/.env: -------------------------------------------------------------------------------- 1 | ES6="environment variables loaded" 2 | -------------------------------------------------------------------------------- /examples/basic/.env: -------------------------------------------------------------------------------- 1 | BASIC="environment variables loaded" 2 | -------------------------------------------------------------------------------- /examples/preload/.env: -------------------------------------------------------------------------------- 1 | PRELOAD="environment variables loaded" 2 | -------------------------------------------------------------------------------- /examples/es6-preload/.env: -------------------------------------------------------------------------------- 1 | ES6PRELOAD="environment variables loaded" 2 | -------------------------------------------------------------------------------- /dotenv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natanfelles/dotenv/master/dotenv.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 4 5 | - 6 6 | - 7 7 | -------------------------------------------------------------------------------- /examples/basic/index.js: -------------------------------------------------------------------------------- 1 | require('../../lib/main').config() 2 | 3 | console.log(process.env.BASIC) 4 | -------------------------------------------------------------------------------- /examples/es6/run_me: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # TODO: make sure node version >1.6.0 4 | 5 | node -r babel/register index.js 6 | -------------------------------------------------------------------------------- /examples/preload/run_me: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # TODO: make sure node version >1.6.0 4 | 5 | node -r ../../config.js index.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | coverage/ 2 | examples/ 3 | test/ 4 | .editorconfig 5 | .npmignore 6 | .travis.yml 7 | CONTRIBUTING.md 8 | dotenv.png 9 | -------------------------------------------------------------------------------- /examples/es6-preload/run_me: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # TODO: make sure node version >1.6.0 4 | 5 | node -r babel/register -r ../../config.js index.js 6 | -------------------------------------------------------------------------------- /examples/preload/index.js: -------------------------------------------------------------------------------- 1 | // dotenv invoked via preload command line argument 2 | // e.g. node -r dotenv/config index.js 3 | // run this example by executing the "run_me" script: ./run_me 4 | console.log(process.env.PRELOAD) 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | var options = {} 3 | process.argv.forEach(function (val, idx, arr) { 4 | var matches = val.match(/^dotenv_config_(.+)=(.+)/) 5 | if (matches) { 6 | options[matches[1]] = matches[2] 7 | } 8 | }) 9 | 10 | require('./lib/main').config(options) 11 | })() 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Coverage directory used by tools like istanbul 2 | coverage 3 | 4 | # Dependency directory 5 | # Commenting this out is preferred by some people, see 6 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 7 | node_modules 8 | 9 | # Users Environment Variables 10 | .lock-wscript 11 | 12 | .DS_Store 13 | -------------------------------------------------------------------------------- /examples/es6/index.js: -------------------------------------------------------------------------------- 1 | // dotenv being imported as an ES6 module 2 | // at this time, the latest node does not support this syntax 3 | // therefore we'll preload babel's register function 4 | // run this example by executing the "run_me" script: ./run_me 5 | 6 | import { config } from '../../lib/main' 7 | 8 | config() 9 | 10 | console.log(process.env.ES6) 11 | -------------------------------------------------------------------------------- /examples/es6-preload/index.js: -------------------------------------------------------------------------------- 1 | // dotenv invoked via preload command line argument along with babel 2 | // e.g. node -r babel/register -r dotenv/config index.js 3 | // run this example by executing the "run_me" script: ./run_me 4 | 5 | import { uptime } from 'os' 6 | 7 | console.log(process.env.ES6PRELOAD) 8 | console.log(`Your computer has been up for ${Math.floor(uptime() / 60 / 60 / 24)} days`) 9 | -------------------------------------------------------------------------------- /test/.env: -------------------------------------------------------------------------------- 1 | NODE_ENV=development 2 | BASIC=basic 3 | 4 | # previous line intentionally left blank 5 | AFTER_LINE=after_line 6 | UNDEFINED_EXPAND=$TOTALLY_UNDEFINED_ENV_KEY 7 | EMPTY= 8 | SINGLE_QUOTES='single_quotes' 9 | DOUBLE_QUOTES="double_quotes" 10 | EXPAND_NEWLINES="expand\nnewlines" 11 | DONT_EXPAND_NEWLINES_1=dontexpand\nnewlines 12 | DONT_EXPAND_NEWLINES_2='dontexpand\nnewlines' 13 | # COMMENTS=work 14 | EQUAL_SIGNS=equals== 15 | RETAIN_INNER_QUOTES={"foo": "bar"} 16 | RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}' 17 | INCLUDE_SPACE=some spaced out string 18 | USERNAME="therealnerdybeast@example.tld" 19 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | 1. Fork it 4 | 2. `npm install` 5 | 3. Create your feature branch (`git checkout -b my-new-feature`) 6 | 4. Commit your changes (`git commit -am 'Added some feature'`) 7 | 5. `npm test` 8 | 6. Push to the branch (`git push origin my-new-feature`) 9 | 7. Create new Pull Request 10 | 11 | ## Testing 12 | 13 | We use [lab](https://github.com/hapijs/lab) and [should](https://github.com/shouldjs/should.js) to write BDD test. Run our test suite with this command: 14 | 15 | ``` 16 | npm test 17 | ``` 18 | 19 | ## Code Style 20 | 21 | We use [standard](https://www.npmjs.com/package/standard) and [editorconfig](http://editorconfig.org) to maintain code style and best practices. Please make sure your PR adheres to the guides by running: 22 | 23 | ``` 24 | npm run lint 25 | ``` 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dotenv", 3 | "version": "4.0.0", 4 | "description": "Loads environment variables from .env file", 5 | "main": "lib/main.js", 6 | "scripts": { 7 | "pretest": "npm run lint", 8 | "test": "lab test/* -r lcov | coveralls", 9 | "lint": "standard", 10 | "postlint": "npm run lint-md", 11 | "lint-md": "standard-markdown" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/motdotla/dotenv.git" 16 | }, 17 | "keywords": [ 18 | "dotenv", 19 | "env", 20 | ".env", 21 | "environment", 22 | "variables", 23 | "config", 24 | "settings" 25 | ], 26 | "readmeFilename": "README.md", 27 | "author": "scottmotte", 28 | "license": "BSD-2-Clause", 29 | "devDependencies": { 30 | "babel": "5.8.23", 31 | "coveralls": "^2.11.9", 32 | "lab": "11.1.0", 33 | "semver": "5.3.0", 34 | "should": "11.1.1", 35 | "sinon": "1.17.6", 36 | "standard": "8.4.0", 37 | "standard-markdown": "2.2.0" 38 | }, 39 | "dependencies": {}, 40 | "engines": { 41 | "node": ">=4.6.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('should') 4 | var cp = require('child_process') 5 | var semver = require('semver') 6 | // var sinon = require('sinon') 7 | var Lab = require('lab') 8 | var lab = exports.lab = Lab.script() 9 | var describe = lab.experiment 10 | // var before = lab.before 11 | // var beforeEach = lab.beforeEach 12 | // var afterEach = lab.afterEach 13 | var it = lab.test 14 | var nodeBinary = process.argv[0] 15 | 16 | describe('config', function () { 17 | describe('preload', function () { 18 | it('loads .env', function (done) { 19 | // preloading was introduced in v1.6.0 so skip test for other environments 20 | if (semver.lt(process.env.npm_config_node_version, '1.6.0')) { 21 | return done() 22 | } 23 | 24 | cp.exec( 25 | nodeBinary + ' -r ../config -e "console.log(process.env.BASIC)" dotenv_config_path=./test/.env', 26 | function (err, stdout, stderr) { 27 | if (err) { 28 | return done(err) 29 | } 30 | 31 | stdout.trim().should.eql('basic') 32 | 33 | done() 34 | } 35 | ) 36 | }) 37 | }) 38 | }) 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Scott Motte 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /lib/main.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var fs = require('fs') 4 | 5 | /* 6 | * Parses a string or buffer into an object 7 | * @param {(string|Buffer)} src - source to be parsed 8 | * @returns {Object} keys and values from src 9 | */ 10 | function parse (src) { 11 | var obj = {} 12 | 13 | // convert Buffers before splitting into lines and processing 14 | src.toString().split('\n').forEach(function (line) { 15 | // matching "KEY' and 'VAL' in 'KEY=VAL' 16 | var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/) 17 | // matched? 18 | if (keyValueArr != null) { 19 | var key = keyValueArr[1] 20 | 21 | // default undefined or missing values to empty string 22 | var value = keyValueArr[2] || '' 23 | 24 | // expand newlines in quoted values 25 | var len = value ? value.length : 0 26 | if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') { 27 | value = value.replace(/\\n/gm, '\n') 28 | } 29 | 30 | // remove any surrounding quotes and extra spaces 31 | value = value.replace(/(^['"]|['"]$)/g, '').trim() 32 | 33 | obj[key] = value 34 | } 35 | }) 36 | 37 | return obj 38 | } 39 | 40 | /* 41 | * Main entry point into dotenv. Allows configuration before loading .env 42 | * @param {Object} options - options for parsing .env file 43 | * @param {string} [options.path=.env] - path to .env file 44 | * @param {string} [options.encoding=utf8] - encoding of .env file 45 | * @returns {Object} parsed object or error 46 | */ 47 | function config (options) { 48 | var path = '.env' 49 | var encoding = 'utf8' 50 | 51 | if (options) { 52 | if (options.path) { 53 | path = options.path 54 | } 55 | if (options.encoding) { 56 | encoding = options.encoding 57 | } 58 | } 59 | 60 | try { 61 | // specifying an encoding returns a string instead of a buffer 62 | var parsedObj = parse(fs.readFileSync(path, { encoding: encoding })) 63 | 64 | Object.keys(parsedObj).forEach(function (key) { 65 | if (!process.env.hasOwnProperty(key)) { 66 | process.env[key] = parsedObj[key] 67 | } 68 | }) 69 | 70 | return { parsed: parsedObj } 71 | } catch (e) { 72 | return { error: e } 73 | } 74 | } 75 | 76 | module.exports.config = config 77 | module.exports.load = config 78 | module.exports.parse = parse 79 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | 5 | ## [Unreleased] 6 | 7 | ## [4.0.0] - 2016-12-23 8 | ### Changed 9 | 10 | - Return Object with parsed content or error instead of false ([#165](https://github.com/motdotla/dotenv/pull/165)). 11 | 12 | 13 | ### Removed 14 | 15 | - `verbose` option removed in favor of returning result. 16 | 17 | 18 | ## [3.0.0] - 2016-12-20 19 | ### Added 20 | 21 | - `verbose` option will log any error messages. Off by default. 22 | - parses email addresses correctly 23 | - allow importing config method directly in ES6 24 | 25 | ### Changed 26 | 27 | - Suppress error messages by default ([#154](https://github.com/motdotla/dotenv/pull/154)) 28 | - Ignoring more files for NPM to make package download smaller 29 | 30 | ### Fixed 31 | 32 | - False positive test due to case-sensitive variable ([#124](https://github.com/motdotla/dotenv/pull/124)) 33 | 34 | ### Removed 35 | 36 | - `silent` option removed in favor of `verbose` 37 | 38 | ## [2.0.0] - 2016-01-20 39 | ### Added 40 | - CHANGELOG to ["make it easier for users and contributors to see precisely what notable changes have been made between each release"](http://keepachangelog.com/). Linked to from README 41 | - LICENSE to be more explicit about what was defined in `package.json`. Linked to from README 42 | - Testing nodejs v4 on travis-ci 43 | - added examples of how to use dotenv in different ways 44 | - return parsed object on success rather than boolean true 45 | 46 | ### Changed 47 | - README has shorter description not referencing ruby gem since we don't have or want feature parity 48 | 49 | ### Removed 50 | - Variable expansion and escaping so environment variables are encouraged to be fully orthogonal 51 | 52 | ## [1.2.0] - 2015-06-20 53 | ### Added 54 | - Preload hook to require dotenv without including it in your code 55 | 56 | ### Changed 57 | - clarified license to be "BSD-2-Clause" in `package.json` 58 | 59 | ### Fixed 60 | - retain spaces in string vars 61 | 62 | ## [1.1.0] - 2015-03-31 63 | ### Added 64 | - Silent option to silence `console.log` when `.env` missing 65 | 66 | ## [1.0.0] - 2015-03-13 67 | ### Removed 68 | - support for multiple `.env` files. should always use one `.env` file for the current environment 69 | 70 | [Unreleased]: https://github.com/motdotla/dotenv/compare/v4.0.0...HEAD 71 | [4.0.0]: https://github.com/motdotla/dotenv/compare/v3.0.0...v4.0.0 72 | [3.0.0]: https://github.com/motdotla/dotenv/compare/v2.0.0...v3.0.0 73 | [2.0.0]: https://github.com/motdotla/dotenv/compare/v1.2.0...v2.0.0 74 | [1.2.0]: https://github.com/motdotla/dotenv/compare/v1.1.0...v1.2.0 75 | [1.1.0]: https://github.com/motdotla/dotenv/compare/v1.0.0...v1.1.0 76 | [1.0.0]: https://github.com/motdotla/dotenv/compare/v0.4.0...v1.0.0 77 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('should') 4 | var sinon = require('sinon') 5 | var Lab = require('lab') 6 | var lab = exports.lab = Lab.script() 7 | var describe = lab.experiment 8 | var before = lab.before 9 | var beforeEach = lab.beforeEach 10 | var afterEach = lab.afterEach 11 | var it = lab.test 12 | var fs = require('fs') 13 | var dotenv = require('../lib/main') 14 | var s 15 | 16 | describe('dotenv', function () { 17 | beforeEach(function (done) { 18 | s = sinon.sandbox.create() 19 | done() 20 | }) 21 | 22 | afterEach(function (done) { 23 | s.restore() 24 | done() 25 | }) 26 | 27 | describe('config', function () { 28 | var readFileSyncStub, parseStub 29 | 30 | beforeEach(function (done) { 31 | readFileSyncStub = s.stub(fs, 'readFileSync').returns('test=val') 32 | parseStub = s.stub(dotenv, 'parse').returns({test: 'val'}) 33 | done() 34 | }) 35 | 36 | it('takes option for path', function (done) { 37 | var testPath = 'test/.env' 38 | dotenv.config({path: testPath}) 39 | 40 | readFileSyncStub.args[0][0].should.eql(testPath) 41 | done() 42 | }) 43 | 44 | it('takes option for encoding', function (done) { 45 | var testEncoding = 'base64' 46 | dotenv.config({encoding: testEncoding}) 47 | 48 | readFileSyncStub.args[0][1].should.have.property('encoding', testEncoding) 49 | done() 50 | }) 51 | 52 | it('reads path with encoding, parsing output to process.env', function (done) { 53 | dotenv.config() 54 | 55 | readFileSyncStub.callCount.should.eql(1) 56 | parseStub.callCount.should.eql(1) 57 | done() 58 | }) 59 | 60 | it('makes load a synonym of config', function (done) { 61 | dotenv.load() 62 | 63 | readFileSyncStub.callCount.should.eql(1) 64 | parseStub.callCount.should.eql(1) 65 | done() 66 | }) 67 | 68 | it('does not write over keys already in process.env', function (done) { 69 | process.env.test = 'test' 70 | // 'val' returned as value in `beforeEach`. should keep this 'test' 71 | dotenv.config() 72 | 73 | process.env.test.should.eql('test') 74 | done() 75 | }) 76 | 77 | it('does not write over keys already in process.env if the key has a falsy value', function (done) { 78 | process.env.test = '' 79 | // 'val' returned as value in `beforeEach`. should keep this '' 80 | dotenv.config() 81 | 82 | process.env.test.should.eql('') 83 | done() 84 | }) 85 | 86 | it('returns parsed object', function (done) { 87 | var env = dotenv.config() 88 | 89 | env.should.not.have.property('error') 90 | env.parsed.should.eql({ test: 'val' }) 91 | done() 92 | }) 93 | 94 | it('returns any errors thrown from reading file or parsing', function (done) { 95 | readFileSyncStub.throws() 96 | 97 | var env = dotenv.config() 98 | env.should.have.property('error') 99 | env.error.should.be.instanceOf(Error) 100 | done() 101 | }) 102 | }) 103 | 104 | describe('parse', function () { 105 | var parsed 106 | before(function (done) { 107 | process.env.TEST = 'test' 108 | parsed = dotenv.parse(fs.readFileSync('test/.env', {encoding: 'utf8'})) 109 | done() 110 | }) 111 | 112 | it('should return an object', function (done) { 113 | parsed.should.be.an.instanceOf(Object) 114 | done() 115 | }) 116 | 117 | it('should parse a buffer from a file into an object', function (done) { 118 | var buffer = new Buffer('BASIC=basic') 119 | 120 | var payload = dotenv.parse(buffer) 121 | payload.should.have.property('BASIC', 'basic') 122 | done() 123 | }) 124 | 125 | it('sets basic environment variable', function (done) { 126 | parsed.BASIC.should.eql('basic') 127 | done() 128 | }) 129 | 130 | it('reads after a skipped line', function (done) { 131 | parsed.AFTER_LINE.should.eql('after_line') 132 | done() 133 | }) 134 | 135 | it('defaults empty values to empty string', function (done) { 136 | parsed.EMPTY.should.eql('') 137 | done() 138 | }) 139 | 140 | it('escapes double quoted values', function (done) { 141 | parsed.DOUBLE_QUOTES.should.eql('double_quotes') 142 | done() 143 | }) 144 | 145 | it('escapes single quoted values', function (done) { 146 | parsed.SINGLE_QUOTES.should.eql('single_quotes') 147 | done() 148 | }) 149 | 150 | it('expands newlines but only if double quoted', function (done) { 151 | parsed.EXPAND_NEWLINES.should.eql('expand\nnewlines') 152 | parsed.DONT_EXPAND_NEWLINES_1.should.eql('dontexpand\\nnewlines') 153 | parsed.DONT_EXPAND_NEWLINES_2.should.eql('dontexpand\\nnewlines') 154 | done() 155 | }) 156 | 157 | it('ignores commented lines', function (done) { 158 | parsed.should.not.have.property('COMMENTS') 159 | done() 160 | }) 161 | 162 | it('respects equals signs in values', function (done) { 163 | parsed.EQUAL_SIGNS.should.eql('equals==') 164 | done() 165 | }) 166 | 167 | it('retains inner quotes', function (done) { 168 | parsed.RETAIN_INNER_QUOTES.should.eql('{"foo": "bar"}') 169 | parsed.RETAIN_INNER_QUOTES_AS_STRING.should.eql('{"foo": "bar"}') 170 | done() 171 | }) 172 | 173 | it('retains spaces in string', function (done) { 174 | parsed.INCLUDE_SPACE.should.eql('some spaced out string') 175 | done() 176 | }) 177 | 178 | it('parses email addresses completely', function (done) { 179 | parsed.should.have.property('USERNAME', 'therealnerdybeast@example.tld') 180 | done() 181 | }) 182 | }) 183 | }) 184 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dotenv 2 | 3 | dotenv 4 | 5 | Dotenv is a zero-dependency module that loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env). Storing configuration in the environment separate from code is based on [The Twelve-Factor App](http://12factor.net/config) methodology. 6 | 7 | [![BuildStatus](https://img.shields.io/travis/motdotla/dotenv/master.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv) 8 | [![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv) 9 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) 10 | [![Coverage Status](https://img.shields.io/coveralls/motdotla/dotenv/master.svg?style=flat-square)](https://coveralls.io/github/motdotla/dotenv?branch=coverall-intergration) 11 | 12 | ## Install 13 | 14 | ```bash 15 | npm install dotenv --save 16 | ``` 17 | 18 | ## Usage 19 | 20 | As early as possible in your application, require and configure dotenv. 21 | 22 | ```javascript 23 | require('dotenv').config() 24 | ``` 25 | 26 | Create a `.env` file in the root directory of your project. Add 27 | environment-specific variables on new lines in the form of `NAME=VALUE`. 28 | For example: 29 | 30 | ```dosini 31 | DB_HOST=localhost 32 | DB_USER=root 33 | DB_PASS=s1mpl3 34 | ``` 35 | 36 | That's it. 37 | 38 | `process.env` now has the keys and values you defined in your `.env` file. 39 | 40 | ```javascript 41 | var db = require('db') 42 | db.connect({ 43 | host: process.env.DB_HOST, 44 | username: process.env.DB_USER, 45 | password: process.env.DB_PASS 46 | }) 47 | ``` 48 | 49 | ### Preload 50 | 51 | If you are using iojs-v1.6.0 or later, you can use the `--require` (`-r`) command line option to preload dotenv. By doing this, you do not need to require and load dotenv in your application code. 52 | 53 | 54 | ```bash 55 | $ node -r dotenv/config your_script.js 56 | ``` 57 | 58 | The configuration options below are supported as command line arguments in the format `dotenv_config_