How to Create and Use .env File in Node JS?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

In this post, we will learn how to create .env file in node js. this lesson will help you how to use .env file in node js. Here you will learn node js .env file lesson. This article will give you simple lesson of node js environment configuration file.

In this simple lesson we will create .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 lesson bellow:

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install dotenv:

npm install dotenv

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}`);

Run App:

node server.js

Result:-

Your App Name is Laravel

Your App Environment is local

Your App Port is 8000

thank you very much for following up with me.

Tags

Recent Posts

Related Posts