How to Use Multiple environments file in Node.js Application

Hey Everyone,

In this article, we will learn how we can set up custom .env files for our node app for different environments using dotenv library.

Let’s Understand the basic details of environments:

Environment variables are one of the most important concepts of Node.js. It allows us to configure our application for multiple environments and allow the application to behave differently for each environment such as development, production, test, and stage.

It is commonly used to configure ports, DB connection host, or handle sensitive data like passwords that our app should connect to for different environment modes or can be used in any part of the code that you think should change based on which environment mode you are running your application on.

Let’s start with creating a node app. Create a folder and name it Multiple_Env. Open the folder with Visual Studio Code. Once inside VS Code, open the terminal and start with the npm init command to create a package.json file.

npm init -y

Install Express.js with the express package:

npm i express

Using dotenv package for Setting our node app for multiple environments.

Let’s first start by installing dotenv package in our application by using the below command:

npm i dotenv

Now that we are done with installing all the libraries we need for our app, let’s Create an index.js file, So go to your app root directory and create it to configure our app for multiple environments using dotenv library.

Now put the below code inside the index.js file:

const express = require('express');
const app = express();
const dotenv = require('dotenv');
const path = require('path');
dotenv.config({
    path: path.resolve(__dirname, `${process.env.NODE_ENV}.env`)
});

const env = {
    NODE_ENV: process.env.NODE_ENV || 'development',
    HOST: process.env.HOST || 'localhost',
    PORT: process.env.PORT || 3000,
    MESSAGE: process.env.MESSAGE || 'Hello, This is from development.'
}
console.log(`NODE_ENV=${env.NODE_ENV}`);
app.get('/', (req, res) => {
    res.send(env.MESSAGE);
});

app.listen(env.PORT, env.HOST, () => {
    console.log(`App Listening On http://${env.HOST}:${env.PORT}`);
});

Let’s Create two .env files development.env and production.env. In which will have the NODE_ENV variable, host, port, and message on which we want our application to run:

Put the below code inside the development.env file:

NODE_ENV=development
HOST=localhost
PORT=3000
MESSAGE=Hello, This is from development.

Put the below code inside the production.env file

NODE_ENV=production
HOST=127.0.0.1
PORT=8000
MESSAGE=Hello, This is from production.

Now we have to set npm run commands for that we will modify the package.json file to add two new tasks, for each environment, here we will set NODE_ENV that we want our application to run and specify the startup file for our app i.e index.js.

Let’s run our app in two different modes (development and production) and see how our app picks up configurations that we set up for these modes.

To run the app in development mode using the command:

npm run dev

Output:

To run the app in production mode run the command:

npm run prod

Output:

If you found this article helpful give some valuable feedback, and comments and share it with your friends.

Also check, How to Upload a Base 64 Image in Nodejs Application.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories