Code Monkey home page Code Monkey logo

dotenv's Introduction

dotenv

dotenv

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

BuildStatus NPM version

"Storing configuration in the environment is one of the tenets of a twelve-factor app. 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 load variables from a .env file into ENV when the environment is bootstrapped."

Brandon Keepers' Dotenv in Ruby

Installation

Add it to your package.json file.

{
  ...
  "dependencies": {
    ...
    "dotenv": "0.4.0"
  }
}

Usage

As early as possible in your application require dotenv and load the .env variables:

var dotenv = require('dotenv');
dotenv.load();

Then, create a .env file in the root directory of your project. Add the application configuration you want. For example:

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
SENDGRID_USERNAME=YOURSENDGRIDUSERNAME
SENDGRID_PASSWORD=YOURSENDGRIDPASSWORDGOESHERE

Whenever your application loads, these variables will be available in process.env:

var sendgrid_username = process.env.SENDGRID_USERNAME;
var secret_key        = process.env.SECRET_KEY;

That's it. You're done.

Custom .env location path

The generally accepted standard is to keep your .env file in the root of your project directory. But you might find yourself wanting to place it elsewhere on your server. Here is how to do that.

var dotenv = require('dotenv');
dotenv._getKeysAndValuesFromEnvFilePath('/custom/path/to/your/.env');
dotenv._setEnvs();

That's it. It ends up being just one extra line of code.

Dotenv.parse

Also added in 0.2.6 the method parse has been exposed. This is how dotenv internally parses multiline buffers or strings into an object to place into the process.env object.

var dotenv  = require('dotenv');
var file    = fs.readFileSync('./config/staging');
var config  = dotenv.parse(file); // passing in a buffer
console.log( typeof config, config ) // object { API : 'http://this.is.a/example' }

Should I commit my .env file?

Try not to commit your .env file to version control. It is best to keep it local to your machine and local on any machine you deploy to. Keep production credential .envs on your production machines, and keep development .envs on your local machine.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Running tests

npm install
npm test

Who's using dotenv

Here's a list of apps/sites/libraries using dotenv. It's in no way a complete list.

Create a pull request and add yours to the list.

dotenv's People

Contributors

bjoshuanoah avatar calleluks avatar davidvermeir avatar jcblw avatar jessefulton avatar jonathanmv avatar maxbeatty avatar motdotla avatar nquinlan avatar sandfox avatar vlad-shatskyi avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.