You are currently viewing Using Dotenv with Node.js

Using Dotenv with Node.js

  • Post author:
  • Post category:Nodejs
  • Post comments:0 Comments
  • Post last modified:February 18, 2024


In Node.js applications, it’s common to use environment variables for configuration settings such as API keys, database URLs, and other sensitive information. However, managing these environment variables across different environments (development, staging, production) can be challenging. This is where Dotenv comes in handy.

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. It simplifies the process of managing environment variables by allowing developers to store them in a single file, separate from the codebase.

In this tutorial, you’ll learn how to use Dotenv with Node.js to manage environment variables in your applications.


Before you begin, make sure you have the following installed:

  • Node.js (and npm, the Node.js package manager) installed on your machine.

Step 1: Setting Up a Node.js Project

First, let’s set up a new Node.js project. Create a new directory for your project and navigate into it:

mkdir dotenv-example
cd dotenv-example

Initialize a new Node.js project using npm:

npm init -y

This will create a package.json file with default values.

Step 2: Installing Dotenv

Next, install Dotenv as a dependency for your project:

npm install dotenv

This will add Dotenv to your project’s node_modules directory and also update the package.json file with the dependency.

Step 3: Creating a .env File

Create a new file named .env in the root directory of your project. This file will contain your environment variables. For example:


Replace localhost, myuser, and mypassword with your actual database host, username, and password.

Step 4: Using Dotenv in Your Application

Now, you can use Dotenv to load the environment variables from the .env file into your Node.js application.

Create a new JavaScript file named app.js in your project directory:

// app.js

// Load environment variables from .env file

// Accessing environment variables
console.log('DB_HOST:', process.env.DB_HOST);
console.log('DB_USER:', process.env.DB_USER);
console.log('DB_PASSWORD:', process.env.DB_PASSWORD);

In this example, we use require('dotenv').config() to load the environment variables from the .env file into process.env. After that, you can access the environment variables using process.env.VARIABLE_NAME.

Step 5: Running Your Application

Now, you can run your Node.js application:

node app.js

You should see the values of the environment variables printed to the console:

DB_HOST: localhost
DB_USER: myuser
DB_PASSWORD: mypassword

Step 6: Ignoring .env Files

Since .env files may contain sensitive information, it’s essential to keep them out of version control. Add .env to your .gitignore file to ensure it’s not accidentally committed to your repository:

# .gitignore



In this tutorial, you learned how to use Dotenv with Node.js to manage environment variables in your applications. Dotenv simplifies the process of loading environment variables from a .env file, making it easier to manage configuration settings across different environments. Now you can use Dotenv to keep your sensitive information secure and separate from your codebase.

Leave a Reply