Open In App

Setting up environment variables in Node.js in a platform independent way

Improve
Improve
Like Article
Like
Save
Share
Report

Environment variables are a good way to ensure security of your precious API keys and secret stuff as well as adaptive code. However, setting up environment variables might be a bit tedious when working on various machines having different Operating Systems. At such times, a portable and project-specific way is needed to use them. Here is a way to configure them using a famous npm module in Node.js.

Step 1: Install dotenv npm package: Open command shell, navigate to the root folder of your project and install dotenv npm package by the following command:

npm install dotenv

If you prefer yarn, you can do:

yarn add dotenv

Step 2: Create a file named .env in the root folder of the project which would store all environment variables

Step 3: Require dotenv package in the project

Require dotenv package only on local setups: In the starter JS file of project, which is generally index.js, import the package as early as possible, using this statement:

if(process.env.NODE_ENV !== "production") {
    require('dotenv').config();
}

Here, we import the package only when NODE_ENV is not set to production, which by default is never set. Thereby,
evaluating undefined !== “production” i.e. True, so dotenv is included. However, production servers have their own ways of setting environment variables, so there isn’t an explicit need to use dotenv on published application. When on production, set the NODE_ENV environment variable to production so that dotenv doesn’t get included. For example, Heroku by default sets this variable to production as stated here, so we don’t have to do it manually, check docs of your hosting provider for further details.

Step 4: Setup environment variables inside the .env file and use them just like normal environment variables: Store the key value pairs of environment variables in the .env file in the following format (without any quotes for keys or values):

KEY = VALUE

For example, your .env file might look like:

DB_KEY = YOUR_ACTUAL_DATABASE_ACCESS_KEY
SECRET_TOKEN = VALUE_OF_YOUR_SECRET_TOKEN
ADMIN_EMAIL = xyz@abc.com

In your code files, you can use these values by referring to their respective keys, For example,

const my_db_key = process.env.DB_KEY
const adminContactMail = process.env.ADMIN_EMAIL

Step 5: Ensure that .env file isn’t tracked by git (For security reasons, if you’re using git) If the project has git setup, open the .gitignore file (Create one, if there isn’t), add .env if not present. This prevents it from getting tracked on to public repositories. If you don’t plan to use git with the project, this step can be skipped. You can share and use this .env file on any machine without spending time setting up environment variables.

Note: There are  several other options like, setting custom path for .env files, etc.


Last Updated : 29 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads