Skip to content

How to Fix dotenv Not Loading Environment Variables

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Fix dotenv Not Loading Environment Variables. We cover key concepts, practical examples, and best practices.

The Problem

process.env.MY_VARIABLE returns undefined even though the variable is defined in a .env file, because dotenv is loaded too late or the file is in the wrong location.

Quick Fix

Step 1: Call dotenv.config() at the very top

The config must run before any code that accesses environment variables:

const express = require('express');
const dotenv = require('dotenv');
const app = express();
dotenv.config();
console.log(process.env.PORT);
undefined

Move dotenv.config() to the very first line:

const dotenv = require('dotenv');
dotenv.config();

const express = require('express');
const app = express();
console.log(process.env.PORT);
3000

Step 2: Check the .env file location

dotenv looks for .env in the current working directory, not the script directory:

dotenv.config();
console.log(process.env.PATH);

If the .env file is in a different directory, specify the path:

const path = require('path');
dotenv.config({ path: path.resolve(__dirname, '../.env') });

Step 3: Verify .env file format

The .env file must follow the correct format:

# WRONG: spaces around =
DB_HOST = localhost

# WRONG: quotes included
DB_HOST="localhost"

# CORRECT
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb

No spaces around = and no quotes unless you want the quotes as part of the value.

Step 4: Check for existing environment variables

dotenv does not override existing variables by default:

dotenv.config();
console.log(process.env.NODE_ENV);

If NODE_ENV is already set in the environment, the .env value is ignored. Override with:

dotenv.config({ override: true });

Step 5: Load dotenv in the entry point for frameworks

For frameworks like Next.js, use their built-in env loading:

npm install dotenv

Then add to the framework's config. In Next.js, env vars from .env.local are loaded automatically.

Prevention

  • Load dotenv.config() at the very top of the entry file.
  • Place .env in the project root directory.
  • Use dotenv with process.cwd() to verify the working directory.
  • Validate environment variables at startup with a helper function.
  • Use .env.example as a template committed to version control.

Common Mistakes with env not loading

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world NODE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does dotenv not work in production?

In production, environment variables are typically set by the hosting platform (Heroku, Vercel, AWS) rather than a .env file. The .env approach is for development. Use process.env.VAR_NAME with fallbacks: const port = process.env.PORT || 3000 so it works in both environments.

Can I use multiple .env files?

Yes. Load multiple files in order of priority:

dotenv.config({ path: '.env.local' });
dotenv.config({ path: '.env' });

Later calls do not override existing variables. Use override: true if needed.

How do I debug what dotenv is loading?

Enable debugging:

require('dotenv').config({ debug: process.env.NODE_ENV !== 'production' });

This logs which variables are loaded from the .env file. You can also log the parsed result: const result = dotenv.config(); console.log(result.parsed);.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro