How to Use Environment Variables in Node.js Application

In this tutorial , you'll learn how to use environment variables in your Node.js application. Environment variables are a great way to store sensitive data, such as API keys or database passwords, in a way that is not visible in your code.  There are so many different ways to use environment variables in Node.js Applications. In this article we cover the following ways.
 

  • 1. Use Environment Variables in Node.js Application via Command Line
  • 2. Use Environment Variables in Node.js Application via .env file

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

Happy Coding !!!

#nodejs #javascript 

How to Use Environment Variables in Node.js Application
1.40 GEEK