In this article I am going to discuss about environment variables,purpose of using it, how to use environment variable,reason to use .env file. So beginning with...
1) What are environment variables and the purpose of using it?
Environment variables are variables whose value can be set outside of your program or code.
The most common purpose of using environment variable is to set up configuration for your project depending upon the environment where your project is hosted. For example if you are hosting a project on a server and there are several projects hosted on that server but inside your code you have used port 3000 and if these port is not available on the server so, you will need to change the port no. inside your code so, to save you here comes the environment variable with the help of which you can easily change the port no. without making changes in your code but how, let's see below.
2) How can we use environment variables in Node?
Let's have a look at the below code image where the web server is been setup in Node and the port is assigned using environment variable 'PORT'.
The most simple way to pass your env variable is using command line as shown in the image above PORT=4000 node app.js
.
Now you can access this env variable(PORT) using a global object "process" of Node which has a property "env" which returns an object containing the user environment.
In the above image you can see that the variable port is assigned as const port=process.env.PORT
. As you can see the port has been assigned the value without making change in code just by passing the value of PORT externally using cmd line.
But think if you have multiple env variables in your project and your are passing those env variable using command line,see the below image won't it be inconvenient,no need to worry here comes .env file to help you but how, let's see below.
3) Why to use .env file?
As discussed above it would be very inconvenient to pass multiple environment variable using command line. So, solution for these is a .env file where you can organize, maintain and modify all of your env variables.
Steps to create and use .env file in Node:
1) Create the .env file in root of your project as shown below in the image and add the env variables to it.
2) Remember to add this .env file to .gitignore file so it doesn't get pushed to git bcoz your .env file contains credentials,passwords etc.
3) Now install a npm package like dotenv which loads environment variables from env file to process.env . If you go with dotenv, after installing it then
require('dotenv').config()
in your project.
4) And now after the third step you will be able to access environment variable using process.env.ENV_VARIABLE_NAME.
In these way you can use .env file in your projects which will help to manage environment variables easily and at one place but wait if you are working in team and if the other teammates clone your git repository they won't have the environment variables as we don't push .env to git and they would have to look in the code which variable have you used or call you. So, in these scenario or genrally you can use .env.example file which would contain the environment variable names with fake value which you can push to git.