Hello,
Today, i we will show you How to Create .env File in Node JS. This article will give you simple example of How to Create .env File in Node JS. you will learn How to Create .env File in Node JS. So let’s follow few step to create example of How to Create .env File in Node JS.
In this easy instance we will build .env file for environment configuration variable like port, app name, database configuration etc. we will use dotenv npm package for setup .env file variable.
Let’s see simple instance bottom.
Step 1:- Create Node App
mkdir my-request-app cd my-request-app
npm init
Step 2:- Install dotenv
npm install dotenv
Step 3:- Create File: server.js
const dotenv = require('dotenv'); dotenv.config(); console.log(`Your App Name is ${process.env.APP_NAME}`); console.log(`Your App Environment is ${process.env.APP_ENV}`); console.log(`Your App Port is ${process.env.APP_PORT}`);
Step 4:- Run App
node server.js