npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dotenv-with-overload

v0.5.1

Published

Loads environment variables from .env

Downloads

3

Readme

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 loads 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.5.1"
  }
}

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' }

Dotenv.load

Added in 0.5.0, the method load returns a boolean to indicate your .env file has been loaded without error. The most likely cause of error is not finding or reading your .env file(s).

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.

Environment-specific .env files

Dotenv will load environment-specific files in addition to .env, based on the pattern .env.<process.env.NODE_ENV>. For example, .env.staging will be loaded if the node environment is set to "staging." This is useful for setting default values and overrides.

Overrides for matching environment variables occurs in this order:

  1. Existing variables set on the machine prior to dotenv
  2. Environment-specific variables
  3. .env

This means for services like Heroku or continuous integration platforms, values will not be overridden by .env files loaded by dotenv.

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.