How to read environment variables in Node.js - TechvBlogs

How to read environment variables in Node.js

This article walks you through creating and using environment variables, leading to a Node.js app you can run anywhere.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 year ago

TechvBlogs - Google News

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.

process.env.PORT
process.env.DB_HOST
process.env.DB_PORT
process.env.DB_USERNAME
process.env.DB_PASSWORD
process.env.DB_NAME

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

PORT=8001
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=nodejs
DB_PASSWORD=nodejs
DB_NAME=env_demo

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:

require('dotenv').config();

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

require('dotenv').config();

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

.env

Thank you for reading this article.

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

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

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

Comments (0)

Comment


Note: All Input Fields are required.