How to read environment variables in Node.js

How to read environment variables in Node.js

In this post we will give you information about How to read environment variables in Node.js. Hear we will give you detail about How to read environment variables in Node.js And how to use it also give you demo for it if it is necessary.

Environment Variables are variables that are set by the Operating System. They are decoupled from application logic. They can be accessed from applications and programs through various APIs.

All applications are typically required to deploy in a development environment before being deployed in the production environment, and it is crucial to be certain that each environment has been set up right. If our production application is using our development credentials, it will be disastrous. 

Here Environment variables come in handy, they are one of the preferred strategies to define and consume environment-specific configurations and are supported by all the major operating systems. 

There is a Node.js library called dotenv that helps you manage and load environment variables. Let’s take a look at the purpose of environment variables, how to use them, and their role in a development environment.

The benefits of using the environment variables can be listed below. 

  • Security: API keys/other secret keys should not be put in plain text in the code to be directly visible. 
  • Flexibility: you can introduce conditional statements based on environments like “If production server then do X else if test server then do Y…”. 
  • Adoption: Popular services like Azure or Heroku support the usage of environment variables. 

There are so many different ways to use environment variables in Node.js Applications. We use one by one method in this article.

1. Use Environment Variables in Node.js Application via Command Line

The simplest way to pass the port into your code is to use it from the command line.

PORT=8001 node server.js

Here we pass the PORT variable in command.

You can pass multiple variables in the command line like this:

PORT=8001 DB_HOST=localhost DB_PORT=3306 DB_USERNAME=nodejs DB_PASS=nodejs DB_NAME=env_demo node server.js

Here we pass the PORT, DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_NAME variables in command.

Here is an example that accesses the PORT, DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_NAME environment variables, which we set in the above code.


Note: process does not require a “require”, it’s automatically available.

2. Use Environment Variables in Node.js Application via .env file

In a Node.js Application, If you have more than one environment variable you can use the .env file. You just need to create a .env file in your project root directory and define environment variables in the .env file.

# .env


You can define any environment variables in the .env file.

Now we need to read these environment variables in our Node.js Application. For the read this Environment variables in Node.js Application we need to use dotenv package.

First, install dotenv the package in your Node.js Application.

npm install dotenv --save

add the following line to the very top of your entry file:


This code automatically loads the .env file into your node.js application. Now you can access your environment variables in your node.js application.


console.log('Port :: ${process.env.PORT}')
console.log('Database Host :: ${process.env.DB_HOST}')
console.log('Database Port :: ${process.env.DB_PORT}')
console.log('Database Username :: ${process.env.DB_USERNAME}')
console.log('Database Password :: ${process.env.DB_PASSWORD}')
console.log('Database Name :: ${process.env.DB_NAME}')

Now create our own .gitignore file and add .env to it and .gitignore will tell source control to ignore the files (or file patterns) that we will list.

# .gitignore


Thank you for reading this article.

Also see: How to Install Node.js and NPM On Ubuntu 20.04

  .       .

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Hope this code and post will helped you for implement How to read environment variables in Node.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US