Next.js: process.env.API_URL is undefined in Client Component / env variable not available in the browser
Only variables prefixed NEXT_PUBLIC_ are exposed to browser code, and they're inlined at build time.
process.env.X is undefined in the browser Next.jsNEXT_PUBLIC_ environment variable undefinedNext.js env variable undefined client sideprocess.env.NEXT_PUBLIC_API_URL is undefinedenvironment variable not available in client component Meaning
Rename to NEXT_PUBLIC_API_URL and rebuild (changes need a new build because values are inlined). Server-only secrets must stay unprefixed and only be read on the server. process.env[key] dynamic access isn't inlined. Check .env.local/platform env scope (Preview vs Production).
Common causes
- Missing
NEXT_PUBLIC_prefix - Variable added after build (not rebuilt)
- Dynamic
process.env[name]access .envfile in wrong place/name- Env not set on the hosting platform
⚡ Quick fix
- Use
NEXT_PUBLIC_for browser variables and rebuild - Access as
process.env.NEXT_PUBLIC_Xliterally - Set env in Vercel/hosting for the right environment
Detailed fix by platform
Next.js
console.log(process.env.NEXT_PUBLIC_API_URL)in a Client Component afternext build
How to diagnose
- Prefix — NEXT_PUBLIC_?
- Build — Rebuilt?
- Platform — Env set?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Conflicting app and page file was found Next.js: ⨯ Conflicting app and page file was found, please remove the conflicting files to continue: "pages/index.tsx" - "app/page.tsx"
- CORS error calling Next.js API route / route handler Browser: Access to fetch at 'https://api.example.com/api/data' from origin 'https://app.example.com' has been blocked by CORS policy (Next.js route handler)
- createContext only works in Client Components Next.js: You're importing a component that needs createContext. It only works in a Client Component but none of its parents are marked with "use client"
Most viewed in Next.js
- NEXT_REDIRECT Next.js: Error: NEXT_REDIRECT (redirect() inside try/catch)
- server-only Next.js: You're importing a component that needs "server-only". That only works in a Server Component
- cookies() should be awaited Next.js 15: Route "/x" used `cookies().get('token')`. `cookies()` should be awaited before using its value
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026