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

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.