Environment variables are one of the most important and core concepts of Nodejs as they allow us to configure our application for multiple environments and allow the application to behave differently for each environment such as development, test, stage, production. It is commonly used to configure ports, DB connection, host that our app should connect to for different environments modes or can be used in any part of the code that you think should change based on which environment mode you are running you application on. Configuring environment variables to a separate file rather than in our node js code enables us to run and point our app to different environments without rebuilding or making changes to our code.
In this Blog we will see how we can setup custom .env files for our node app for different environments using dotenv library
Let’s start with creating a node app first
Setting up our node app ⚙
To create a node app start with npm init command to create package.json file
Install Express.js with express package
npm express --save
Using dotenv package for Setting our node app for multiple environments
Let’s first start by installing dotenv package in our application by using below command
npm i dotenv --save
Now that we are done with installing all the libraries we need for our app, let’s jump on to creating our node app and configure our app for multiple environments usinf dotenv library.
Steps to setting up node app for multiple environments:
1️⃣ Create two .env files development.env and production.env
2️⃣ Create a config file to read and set the environment from .env files we created in first step
3️⃣ Create a server.js file as a startup file for our nodeapp
4️⃣ Set npm runscripts for multiple environments in package.json.
🔸 Creating .env files
Let’s create 2 new files development.env and production.env, one for each environment, which will have the NODE_ENV variable, host and port on which we want our application to run:
The config file will use dotenv library that will allow us to load the desired .env file through the config() method and the path object.Once we read the file we will set the variable to the values of .env file and export them to use them further in server.js filehttps://medium.com/media/acb3a7d78264f258af02a9a00fe66b60
🔸 Creating a server.js file
Now that we have our different .env and config ready, we will now create the starting point of our app in server.js file, server.js file will import the config.js file and run our app basedon this config.https://medium.com/media/d1095743c86953dff5df2b5844798020
🔸 Set npm run commands:
We will modify 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 server.jshttps://medium.com/media/71eef957f877c68b7091dfad192c965c
Now let us run our app in two different modes (development and production) and see how our app picks up configurations that we setup for these modes.
To run the app in development mode use the command:
npm run dev
What happens here ? : when we run the command npm run dev,NODE_ENV is set to development frompackage .json script section, in config.js file, dotenv.config function reads thedevelopment.env file and set the environment variables as specified in the development.env file, server.js file read these set values from config.js file and executes the program accordingly
You will see that our app picks the configuration that we set up in development.env file i.e PORT : 3000 and HOST : localhost
To run the app in production mode run the command
npm run prod
You will see that our app picks the configuration that we set up in production.env file i.e PORT: 8000 and HOST : 127.0.0.1
Configuring different environments modes for our node app enables us to specify different config for each mode and allow us to run our application without the need to rebuild our app or make changes in our code.
We use analytics to understand the usage, to improve user experience and to measure the performance of our website. We anonymise any information we may collate so we can’t identify you personally.