"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
.envfile into ENV when the environment is bootstrapped."
Brandon Keepers' Dotenv in Ruby
UsePut this require statement as early in your app code as possible. Ideally before any other require statements are run.
require('localenv'); // everything else after
.env filesLocalenv will ONLY load
.envfiles in development. Development is defined by the
NODE_ENVenvironment variable. Any value other than
productionis considered development.
DEBUG=app* # this is a comment DB_URL=postgres://localhost:5432/my_database # api requests API_URL=http://api.myapp.com
.env.localAdditionally, you can create an
.envwhich contains any overrides or additional environment variables.
If the project's
.envfile has a value of
DEBUG=app*but you want to point to a different API server.
.gitignorefile to ensure that no developer commits their local overrides to the repo.
Load orderPrecensece 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.envhas the highest precedence and
Should I commit my .env file?YES
>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
.envfile 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.localfile which is personal to your machine and may be used to temporarily override the defaults provided by the project
Avoid automatic loading and injectionBy default,
require('localenv')will load and inject the environment values found in the
.envfile. If you wish to avoid automatic loading and perform the injection yourself (from custom files), you can
var localenv = require('localenv/noload'); // load and inject the variables into process.env localenv.inject_env('/path/to/.env/file'); localenv.inject_env('/path/to/.env.test');