Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: motdotla/dotenv
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.0
Choose a base ref
...
head repository: motdotla/dotenv
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.2.0
Choose a head ref
  • 14 commits
  • 8 files changed
  • 7 contributors

Commits on May 5, 2015

  1. Copy the full SHA
    4795044 View commit details

Commits on May 11, 2015

  1. Merge pull request #64 from kemitchell/spdx-license

    Use a Standard-Compliant License Identifier
    jcblw committed May 11, 2015

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    11eefb6 View commit details
  2. Document silent option

    motdotla committed May 11, 2015
    Copy the full SHA
    533346d View commit details
  3. Copy the full SHA
    236017f View commit details
  4. Copy the full SHA
    7ab7072 View commit details
  5. Merge pull request #66 from motdotla/sm-docs

    Document silent option
    motdotla committed May 11, 2015
    Copy the full SHA
    c906f8d View commit details
  6. Merge pull request #67 from motdotla/sm-readme-fix

    Fix issue #59 - example should be double quotes
    motdotla committed May 11, 2015
    Copy the full SHA
    b21b575 View commit details

Commits on May 18, 2015

  1. Copy the full SHA
    5fb5c31 View commit details

Commits on May 20, 2015

  1. Copy the full SHA
    abefbcb View commit details

Commits on May 21, 2015

  1. Merge pull request #71 from anandkumarpatel/add-space-test

    add test that sets ENV to a string with spaces
    maxbeatty committed May 21, 2015
    Copy the full SHA
    7b83ab6 View commit details

Commits on Jun 18, 2015

  1. add preload feature, update linting docs and cmds

    Max Beatty committed Jun 18, 2015

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    626c30b View commit details

Commits on Jun 19, 2015

  1. skip preload tests in lesser versions of node

    Max Beatty committed Jun 19, 2015

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e5cd421 View commit details

Commits on Jun 21, 2015

  1. Merge pull request #73 from motdotla/preload

    preload module
    jcblw committed Jun 21, 2015
    Copy the full SHA
    7ef0347 View commit details
  2. bumping version

    Jacob Lowe committed Jun 21, 2015
    Copy the full SHA
    940d101 View commit details
Showing with 113 additions and 32 deletions.
  1. +2 −2 Contributing.md
  2. +47 −22 README.md
  3. +11 −0 config.js
  4. +3 −3 lib/main.js
  5. +6 −5 package.json
  6. +1 −0 test/.env
  7. +38 −0 test/config.js
  8. +5 −0 test/main.js
4 changes: 2 additions & 2 deletions Contributing.md
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ npm test

## Code Style

We use [eslint](http://eslint.org) and [editorconfig](http://editorconfig.org) to maintain code style and best practices. Please make sure your PR adheres to the guides by running:
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:

```
gulp lint
npm run lint
```
69 changes: 47 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,19 +4,19 @@

Dotenv loads environment variables from `.env` into `ENV` (process.env).

[![BuildStatus](https://img.shields.io/travis/motdotla/dotenv.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv)
[![BuildStatus](https://img.shields.io/travis/motdotla/dotenv/master.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv)
[![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)

> "Storing [configuration in the environment](http://www.12factor.net/config)
> is one of the tenets of a [twelve-factor app](http://www.12factor.net/).
> Anything that is likely to change between deployment environments–such as
> resource handles for databases or credentials for external services–should be
> "Storing [configuration in the environment](http://www.12factor.net/config)
> is one of the tenets of a [twelve-factor app](http://www.12factor.net/).
> Anything that is likely to change between deployment environments–such as
> resource handles for databases or credentials for external services–should be
> extracted from the code into environment variables.
>
> But it is not always practical to set environment variables on development
> machines or continuous integration servers where multiple projects are run.
> Dotenv loads variables from a `.env` file into ENV when the environment is
> But it is not always practical to set environment variables on development
> machines or continuous integration servers where multiple projects are run.
> Dotenv loads variables from a `.env` file into ENV when the environment is
> bootstrapped."
>
> [Brandon Keepers' Dotenv in Ruby](https://github.com/bkeepers/dotenv)
@@ -35,7 +35,7 @@ As early as possible in your application, require and load dotenv.
require('dotenv').load();
```

Create a `.env` file in the root directory of your project. Add
Create a `.env` file in the root directory of your project. Add
environment-specific variables on new lines in the form of `NAME=VALUE`.
For example:

@@ -57,21 +57,47 @@ db.connect({
});
```

### Preload

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.


```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
```

## Config

`config` will read your .env file, parse the contents, and assign it to
`config` will read your .env file, parse the contents, and assign it to
`process.env` - just like `load` does. You can additionally, pass options to
`config`.

Note: `config` and `load` are synonyms. You can pass options to either.

### Options

#### Silent

Default: `false`

Dotenv outputs a warning to your console if missing a `.env` file. Suppress
this warning using silent.

```js
require('dotenv').config({silent: true});
```

#### Path

Default: `.env`

You can specify a custom path if your file containing environment variables is
You can specify a custom path if your file containing environment variables is
named or located differently.

```js
@@ -82,7 +108,7 @@ require('dotenv').config({path: '/custom/path/to/your/env/vars'});

Default: `utf8`

You may specify the encoding of your file containing environment variables
You may specify the encoding of your file containing environment variables
using this option.

```js
@@ -91,8 +117,8 @@ require('dotenv').config({encoding: 'base64'});

## 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
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
@@ -111,7 +137,7 @@ The parsing engine currently supports the following rules:
- lines beginning with `#` are treated as comments
- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
- single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`)
- new lines are expanded if in double quotes (`MULTILINE='new\nline'` becomes
- new lines are expanded if in double quotes (`MULTILINE="new\nline"` becomes

```
{MULTILINE: 'new
@@ -128,9 +154,9 @@ BASIC=basic
TEST=$BASIC
```

Parsing that would result in `{BASIC: 'basic', TEST: 'basic'}`. You can escape
variables by quoting or beginning with `\` (e.g. `TEST=\$BASIC`). If the
variable is not found in the file, `process.env` is checked. Missing variables
Parsing that would result in `{BASIC: 'basic', TEST: 'basic'}`. You can escape
variables by quoting or beginning with `\` (e.g. `TEST=\$BASIC`). If the
variable is not found in the file, `process.env` is checked. Missing variables
result in an empty string.

```
@@ -151,9 +177,9 @@ TEST=example node -e 'require("dotenv").config();'

### Should I commit my .env file?

No. We **strongly** recommend against committing your .env file to version
control. It should only include environment-specific values such as database
passwords or API keys. Your production database should have a different
No. We **strongly** recommend against committing your .env file to version
control. It should only include environment-specific values such as database
passwords or API keys. Your production database should have a different
password than your development database.

## Contributing
@@ -170,4 +196,3 @@ Here's just a few of many repositories using dotenv:
* [google-oauth2-service-account](https://github.com/jacoblwe20/google-oauth2-service-account)
* [kibble](https://github.com/motdotla/kibble)
* [github-streaker](https://github.com/motdotla/github-streaker)

11 changes: 11 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function () {
var options = {}
process.argv.forEach(function (val, idx, arr) {
var matches = val.match(/^dotenv_config_(.+)=(.+)/)
if (matches) {
options[matches[1]] = matches[2]
}
})

require('./lib/main').config(options)
})()
6 changes: 3 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -14,15 +14,15 @@ module.exports = {
var silent = false

if (options) {
if (options.silent) {
silent = options.silent
}
if (options.path) {
path = options.path
}
if (options.encoding) {
encoding = options.encoding
}
if (options.silent) {
silent = options.silent
}
}

try {
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "dotenv",
"version": "1.1.0",
"version": "1.2.0",
"description": "Loads environment variables from .env file",
"main": "lib/main.js",
"scripts": {
"test": "lab test/* --coverage && standard"
"test": "lab test/* --coverage && standard",
"lint": "standard"
},
"repository": {
"type": "git",
@@ -21,13 +22,13 @@
],
"readmeFilename": "README.md",
"author": "scottmotte",
"license": "BSD",
"license": "BSD-2-Clause",
"devDependencies": {
"lab": "^5.3.0",
"semver": "^4.3.6",
"should": "4.4.2",
"sinon": "1.12.2",
"standard": "^2.10.0"
},
"dependencies": {
}
"dependencies": {}
}
1 change: 1 addition & 0 deletions test/.env
Original file line number Diff line number Diff line change
@@ -16,3 +16,4 @@ DONT_EXPAND_NEWLINES_2='dontexpand\nnewlines'
EQUAL_SIGNS=equals==
RETAIN_INNER_QUOTES={"foo": "bar"}
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
INCLUDE_SPACE=some spaced out string
38 changes: 38 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

require('should')
var child_process = require('child_process')
var semver = require('semver')
// var sinon = require('sinon')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var describe = lab.experiment
// var before = lab.before
// var beforeEach = lab.beforeEach
// var afterEach = lab.afterEach
var it = lab.test
var nodeBinary = process.argv[0]

describe('config', function () {
describe('preload', function () {
it('loads .env', function (done) {
// preloading was introduced in v1.6.0 so skip test for other environments
if (semver.lt(process.env.npm_config_node_version, '1.6.0')) {
return done()
}

child_process.exec(
nodeBinary + ' -r ../config -e "console.log(process.env.BASIC)" dotenv_config_path=./test/.env',
function (err, stdout, stderr) {
if (err) {
return done(err)
}

stdout.trim().should.eql('basic')

done()
}
)
})
})
})
5 changes: 5 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -195,5 +195,10 @@ describe('dotenv', function () {
parsed.RETAIN_INNER_QUOTES_AS_STRING.should.eql('{\"foo\": \"bar\"}')
done()
})

it('retains spaces in string', function (done) {
parsed.INCLUDE_SPACE.should.eql('some spaced out string')
done()
})
})
})