June 6, 2022

How to Load Environment Variables on a Development Machine

What are .env files and how to use them on Mac and Linux

How to Load Environment Variables on a Development Machine

Environment variables are values that may need to be different depending on where the application is running. Typically, each environment will have its own set of environment variables. There is also one other use case - using them as runtime variables - but that is out of the scope of this article.

Having different files with settings for different environments can be a very useful way to organise your development. Many developers run a three environment system:

  1. Development/Local environment
  2. Testing/Staging environment
  3. Production environment

As a result, a typical organisation for a small project might look like this

project
│   README.md    
│
└───src
│   │   index.js
│
└───config   
│   │   development.env
│   │   staging.env
│   │   production.env
│   
└───node_modules
│   │   ...

In more complex scenarios staging.env and production.env might not exist in your project because your dev ops team will have other ways of storing the environment variables.

What does a .env file look like

.env (pronounced "dot env") files use a simple key value format. The example below shows a development.env file that sets the values for DATABASE_HOST and DATABASE_PORT

DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
development.env

Environment Variable Scope

The most important thing to understand about environment variables is that they have a very limited scope. If you open two terminal windows, there is a system on your computer that will set a list of default environment variables for both of them.

You can see the environment variables in each terminal window by typing

printenv

You can see the value for a specific environment variable by adding the name of the environment variable you are interested in:

printenv SHELL
The SHELL environment variable is a default environment variable which will return the path to the command line intepreter you are using

If you enter the name of an environment variable that does not exist, nothing will be printed.

You can use the export command to set new environment variables. Try opening two terminal windows, and adding an new environment variable to one of them using:

export hello=world

Now in each terminal use

printenv hello

You will find that the environment variable only exists in one terminal window and not the other.

This same principle applies to your node applications. If you were to run the same node application in both of your terminal windows, one would have access to the environment variable named hello and the other would not.

How to set lots of environment variables at once

Over time your project will usually start to require a lot of environment variables. For node projects, many people will tell you to use the dotenv package on npm (listed here for clarity), but I recommend against it.

dotenv
Loads environment variables from .env file. Latest version: 16.0.1, last published: a month ago. Start using dotenv in your project by running `npm i dotenv`. There are 28313 other projects in the npm registry using dotenv.

In order to load your environment variables from a file open a terminal window and run this command:

export $(cat config/development.env | xargs)

This takes each line of your development.env file and prepends export before executing it in the terminal. Thus allowing you to export many environment variables in a single line.