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

29 Oct, 2020
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.
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
Installation of Node.js on Windows
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React