How to Fix dotenv Not Loading Environment Variables
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
.envin the project root directory. - Use
dotenvwithprocess.cwd()to verify the working directory. - Validate environment variables at startup with a helper function.
- Use
.env.exampleas a template committed to version control.
Common Mistakes with env not loading
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro