What is dotenv?
The dotenv npm package is used to load environment variables from a .env file into process.env, providing a convenient way to configure your application's environment during development. It helps in managing sensitive credentials and configuration options by keeping them out of the codebase.
What are dotenv's main functionalities?
Basic Configuration
This is the simplest use case for dotenv. By calling `require('dotenv').config();`, dotenv reads the .env file, parses the contents, and loads them into `process.env`. After this, environment variables can be accessed using `process.env.VAR_NAME`.
require('dotenv').config();
Custom Path
If your .env file is not located in the root directory or you have multiple .env files, you can specify a custom path to your .env file using the `path` option.
require('dotenv').config({ path: '/custom/path/to/.env' });
Debugging
To assist in debugging, you can enable debug output by setting the `debug` option to `true`. This will log any errors to the console while reading the .env file.
require('dotenv').config({ debug: process.env.DEBUG });
Other packages similar to dotenv
dotenv-expand
dotenv-expand is an extension for dotenv. It allows you to have environment variables in your .env file that reference other environment variables, similar to variable expansion in Unix shell scripts. It's useful when you need to reduce redundancy in your .env files.
cross-env
cross-env allows you to set and use environment variables across platforms without worrying about platform-specific differences in how environment variables are set. Unlike dotenv, which is focused on loading variables from a file, cross-env is more about providing scripts with environment variables in a cross-platform way.
env-cmd
env-cmd is another npm package that allows you to specify a file containing environment variable definitions and then run a given command using those variables. It's similar to dotenv but is more focused on injecting environment variables into the command line for scripts, rather than loading them into `process.env`.
dotenv
Dotenv loads environment variables from .env into ENV (process.env). It is a superior alternative to nconf and other variants.
"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.2.8"
}
}
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.
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);
console.log( typeof config, config )
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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - 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.