14-Sep-2022
.
Admin
Hi Friends,
In this tute, we will discuss how to get the environment variable in node js. This article goes into detail on how to get environment variable in node.js. you will learn node.js get environment variable. if you want to see an example of node js get environment variable then you are in the right place. Follow the below tutorial step to get the environment variable.
I will give you a simple example of get the environment variable in node js. In this example get environment variable using dotenv package in node js.
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.
So let's start following example:
Step 1: Install Node JS
This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:
mkdir my-app
cd my-app
npm init
Step 2: Install dotenv Package
npm install dotenv --save
Step 3: Create .env File
Create .env file and following code:
.env
ENV_MESSAGE = "Hello Welcome to Nicesnippets.com"
Step 4: Update server.js file
server.js
// require package
require('dotenv').config()
// print env variable
console.log(process.env.ENV_MESSAGE)
Output:
Hello Welcome to Nicesnippets.com
I hope it can help you...
#Node JS