Node dotenv and Environment Variables
Learn to use dotenv and environment variables to configure a node application.
Table of Contents 📖
- What are Environment Variables?
- What is the Purpose of Environment Variables?
- Environment Variables and Node
- Accessing Environment Variables in Node
- Providing Node Environment Variables
- Node and .env Files
- .env File Security
- Accessing .env File Contents with dotenv
- Multiple .env Files
- Summary
What are Environment Variables?
Environment variables are variables set by the operating system that help form the environment computer programs run in. They are made up of key/value pairs. We can get a list of our environment variables by opening up a command prompt and typing in set. A commonly used environment variable is the PATH variable. The PATH variable is an environment variable that contains an ordered list of paths that the operating system will look at when executing a command.
What is the Purpose of Environment Variables?
Environment variables are useful for separating programming and configuration. This makes it easier to deploy an application in different environments. For example, the database configuration settings would be different for an application in development versus production. These configuration settings can be stored as environment variables. Environment variables also allow for the protection of configuration settings. For example, API keys should not be hard coded into an application out in production. Instead, API keys should be added as environment variables from a closed environment.
Environment Variables and Node
Environment variables can be used to configure a Node application. Common uses of environment variables are setting the port of an application, configuring database connection settings, setting the development environment as development or production, etc.
Accessing Environment Variables in Node
In Node, environment variables are accessed using the process object. Process is a global object injected during runtime that represents a Node process. It contains properties used to interact with the Node process. One of these properties is env.
process.env
The property env provides access to all existing environment variables when the Node process it represents boots up. For example, we can access the PATH environment variable with the Node process object.
console.log(process.env.PATH);
Providing Node Environment Variables
undefined
As process.env is an object, a new property can be set the same way a normal object property would be.
process.env.PORT = 1234;
console.log(process.env.PORT); // 1234
However, environment variables provided to Node this way are only set during execution of the Node process. A better way to provide environment variables is through a configuration variable. For example, in a Bash shell, we can provide an environment variable when running a Node program.
PORT=1234 node index.js
We can then access this port variable with process.env.PORT inside the index.js file.
console.log(process.env.PORT); // 1234
However, the best way to provide environment variables to a Node process is through an environment variables file (.env file).
Node and .env Files
A .env file is a text file that houses a set of key value pairs. For example, we could place the values for our database username, password, and a Google maps API key inside a .env file like so.
DB_USERNAME=root
DB_PASSWORD=toor
GMAP_API=SXLSKEDUGXF45DXG
Environment variable files are great for acheiving project specific configuration as they allow us to specify a variety of different environment variables, their values, all while keeping them hidden from the outside world.
.env File Security
Environment variable files should not be shared with anyone or visible in the source code as they commonly contain sensitive information such as database passwords and API keys. As such, environment variable files should also not be pushed to any form of version control. To prevent this with Git, we just need to create a .gitignore file and place .env inside it.
.env
Accessing .env File Contents with dotenv
To access these environment variables at runtime with our Node process, we need to use the npm package dotenv. dotenv is an npm package that loads environment variables from a .env file into process.env. To use dotenv, we first need to install it.
npm install dotenv
To access these environment variables with dotenv we just place the following code inside any file we want to use the environment variables in.
require('dotenv').config();
console.log(process.env.DB_USERNAME); // root
console.log(process.env.DB_PASSWORD); // toor
console.log(process.env.GMAP_API); // SXLSKEDUGXF45DXG
Multiple .env Files
It isn't uncommon to have multiple environment variable files. For example, one for development (.env.development), one for production (.env.production), etc. This can be implemented using dotenv.
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}`});
Now, we can supply a NODE_ENV variable through a bash terminal. This will determine which configuration file gets served.
NODE_ENV=development node index.js
Summary
But there we have it! Environment variables are great for application configuration and definitely a useful tool to have! If this article was helpful, please consider donating at the top of the page, sharing this article, and subscribing to my YouTube channel WittCode!