Choorai
Common Error

Fixing Environment Variable Errors

Getting "process.env.API_KEY is undefined"? This happens when environment variables are not properly configured.

TL;DR

1) Create a .env file 2) Add VITE_ or NEXT_PUBLIC_ prefix to variable names 3) Restart the server

주의

process.env.API_KEY is undefined

원인

The environment variable is not set, or you're using a variable that is not accessible from the frontend.

해결책
  1. Create a .env file in the project root
  2. Vite: Add the VITE_ prefix to variable names
  3. Next.js: Add the NEXT_PUBLIC_ prefix to variable names
  4. Restart the server

Setup by Framework

Vite React + Vite

.env
# Accessible from frontend (VITE_ prefix required!)
VITE_API_URL=https://api.example.com
VITE_PUBLIC_KEY=pk_test_xxx

# Not accessible from frontend (server-only)
DATABASE_URL=postgres://...
SECRET_KEY=your-secret

Usage: import.meta.env.VITE_API_URL

Next.js

.env.local
# Accessible from frontend (NEXT_PUBLIC_ prefix required!)
NEXT_PUBLIC_API_URL=https://api.example.com

# Server-only
DATABASE_URL=postgres://...

Usage: process.env.NEXT_PUBLIC_API_URL

Python FastAPI

.env
DATABASE_URL=postgres://user:pass@localhost/db
SECRET_KEY=your-secret-key
DEBUG=true
config.py
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    database_url: str
    secret_key: str
    debug: bool = False

    class Config:
        env_file = ".env"

settings = Settings()

Add to .gitignore!

.env files contain sensitive information. Make sure to add them to .gitignore so they don't get uploaded to GitHub.

.gitignore
# Environment variable files
.env
.env.local
.env.*.local

Prerequisites

  • You can edit the project root .env file.
  • You know your framework runtime (Vite/Next.js/FastAPI).
  • You can restart app processes and inspect logs.

Validation

  1. Vite exposes expected values via import.meta.env.VITE_*.
  2. Next.js exposes expected values via process.env.NEXT_PUBLIC_* in browser code.
  3. Server-only secrets are not exposed in client bundles.

Troubleshooting

  • Confirm .env file path is project root.
  • Verify required prefixes (VITE_, NEXT_PUBLIC_) are present.
  • Restart dev/build process after changing environment variables.

References

Related Articles

Last updated: February 22, 2026 · Version: v0.0.1

Send Feedback

Opens a new issue page with your message.

Open GitHub Issue