load environment variables from .env files

Downloads in past


3610.2.210 years ago10 years agoMinified + gzip package size for localenv in KB


localenv Build Status
Load environment variables from .env into process.env in development.
"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


Put this require statement as early in your app code as possible. Ideally before any other require statements are run.

// everything else after

.env files

Localenv will ONLY load .env files in development. Development is defined by the NODE_ENV environment variable. Any value other than production is considered development.
Example .env files

# this is a comment

# api requests


Additionally, you can create an .env.local file alongside .env which contains any overrides or additional environment variables.
If the project's .env file has a value of DEBUG=app* but you want to point to a different API server.

Note: Add .env.local to your .gitignore file to ensure that no developer commits their local overrides to the repo.

Load order

Precensece is followed in this order. If a particular variable is already set by the time a file is loaded, the env var in the file will be ignored. In this way, process.env has the highest precedence and .env the lowest.
  • process.env
  • .env.local
  • .env

Should I commit my .env file?

>This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data
>Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server. 1

The .env file is a great localtion to document all of the environment settings used by your app. As they change, this file will continue to reflect the defaults.
Note: Do not commit your .env.local file which is personal to your machine and may be used to temporarily override the defaults provided by the project .env file.

Avoid automatic loading and injection

By default, require('localenv') will load and inject the environment values found in the .env file. If you wish to avoid automatic loading and perform the injection yourself (from custom files), you can require('localenv/noload')
var localenv = require('localenv/noload');

// load and inject the variables into process.env




  • Based on work from the dotenv module which is also MIT licensed.
  • Some ideas from dotenv ruby as well.