Node.js & Environment Variables

Sharing is Caring

Environment variables, also known as env, allow us to have applications we build behave differently based on the environment that they are running in. Well designed applications use environment variables and allow us to rapidly deploy to new environments.

Why use Environment Variables

They allow us to change parts of the application as needed for the different stages or environments the application runs in. Environment variables also allow us to change the environment without necessarily having to rebuild the code.

And finally, using env allows us to keep our secrets secret without them being checked into source code.

When to Use Environment Variables

Environment variables should be used for any place in the code that will change based on the environment being used. A perfect example of this is your development database will be different than your production database.

Other use cases might include any of the following:

  • path or folder files are located in
  • different credentials or api keys for development, staging, production
  • something that is likely to change (mailing addresses, currency conversation factors, etc).

I also like to use env in place of magic numbers as I find they can be really self documenting.

Using Environment Variables

Using an environment variable in node is pretty easy, they become part of the process.env object. So for example, to use it for a database username I would do something like this.

const mysql = require('mysql')

const connection = mysql.createConnection({
  host: 'localhost',
  user: process.env.DATABASE_USER_NAME, // notice i'm using it here!
  password: 'password',
  database: 'database name'
});

By convention, these variable should be all capitalized and use the underscore “_” between words.

Normally you would have dozens of variables you want to set this way, so you wouldn’t want to do this but you can pass variables through the terminal when calling your script.

For example,

DATABASE_USER_NAME=root node index.js

As you can likely imagine this produces a giant mess when you are passing in a dozen variables and becomes really hard to follow and to maintain. Some people use npm scripts to try and make this easier but it isn’t really that much easier.

.env files are a lot better solution. This works by creating a single .env file in the root of the project and adding the relevant variables and values to it. Here’s an example file

NODE_ENV=development
DATABASE_HOST=192.168.0.1
DATABSE_PORT=22
DATABASE_USER_NAME=root
DATABASE_PASSWORD=P@ssw0rd*

As you can see this is pretty nice and easy to see what the various values are. Make sure that you update your .gitignore file to exclude the .env file.

To read the env files, we need to install a node package called dotenv which can be installed using “npm i dotenv”.

At the top of your entry file (likely it’s index.js or application.js) you’ll need to require the new package and call dotenv.config(). Like this:

const dotenv = require('dotenv')
dotenv.config()

// my awesome code goes here....

When we run our code we can simply call it the exact same way as before (ie: “node index.js”) and it would use the environment variables as we set them.

Hope this helps!

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.