You created a .env file with API keys and variables, but process.env.YOUR_VAR returns undefined in your app. Or it works locally but not in production.

Different frameworks and environments load .env files differently. Make sure you're using the correct prefix, loading method, and that variables are exposed to the client properly.

Step-by-Step Guide

1

React (Create React App): Variables MUST start with REACT_APP_ (e.g., REACT_APP_API_KEY)

2

Next.js: Use NEXT_PUBLIC_ prefix for client-side variables

3

Node.js: Install dotenv package and call require('dotenv').config() at app entry point

4

Vite: Use VITE_ prefix for client-side variables

5

After changing .env: ALWAYS restart dev server (hot reload doesn't pick up .env changes)

6

Check .env is in project root (same level as package.json)

7

Verify .env is not in .gitignore (wait, it should be! Use .env.example for templates)

8

Production: Set environment variables in your hosting platform (Vercel, Heroku, etc.)

9

Never commit .env to git - use .env.example with dummy values

Found an issue with this solution?

Related Topics

dotenv not loading productionenv variables nextjs builddocker env file not working