Node JS Environment Variables: Setting up Environment Variables in Node JS

è Environment Variable are access all over the application

è These are like Global variables

è We can use them in configurations

è By default environment variables will be store in process.env

è If we want to set up environment variables and use them we need to create one file under the main folder called .env

è We can add environment variables in the .env file.

è To access this values we need install dotenv package, This can be done with npm install dotenv statement

è After installing page need to include it in file -> require(‘dotenv’).config(). Will load the dotenv package

Example:

In .env file you can add any constants, Below is the example constants I’m Using.

USER_ID = 12

In app.js file, I’ll be using the above mentioned environment value

require("dotenv").config();

console.log("welcome");

console.log(process.env.USER_ID);

 

OUTPUT:

Welcome

12 

Comments

Popular posts from this blog

How to Use Crome Developer Tools: Crome Developer Tools