ItSolutionStuff.com

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

By Hardik Savani • May 14, 2021
Node JS

Hi,

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

In this simple example 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 example 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

Output:

Your App Name is Laravel

Your App Environment is local

Your App Port is 8000

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Node JS Http Request with Headers Example

Read Now →

Node JS Make HTTP Put Request Example

Read Now →

Node JS Make HTTP Delete Request Example

Read Now →

Node JS Make Http Get Request with Parameters Example

Read Now →

How to make an HTTP POST request in Node JS?

Read Now →

Node JS Mysql Connection Example

Read Now →

Node JS Multer Rename Uploaded File Example

Read Now →

Node js Express Multiple Image Upload using Multer Example

Read Now →

File Upload in Node JS using Multer Example

Read Now →

Laravel Authenticate User in NodeJS with Socket io using JWT

Read Now →