How to Create and Use .env File in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Node JS Http Request with Headers Example
- Node JS Make HTTP Put Request Example
- Node JS Make HTTP Delete Request Example
- Node JS Make Http Get Request with Parameters Example
- How to make an HTTP POST request in Node JS?
- Node JS Mysql Connection Example
- Node JS Multer Rename Uploaded File Example
- Node js Express Multiple Image Upload using Multer Example
- File Upload in Node JS using Multer Example
- Laravel Authenticate User in NodeJS with Socket io using JWT