published fresh until

Why is my env variable undefined in the browser in Nuxt?

Summary As of November 2026, browser runtime config exposes public keys and Nuxt's reserved app keys. Declare public.apiBase in nuxt.config.ts, override it with NUXT_PUBLIC_API_BASE, and read useRuntimeConfig().public.apiBase. Start with runtimeConfig.public.apiBase

Not for Vite import.meta.env configuration.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Declare runtimeConfig.public.apiBase in nuxt.config.ts.

Short answer

Only runtimeConfig.public and Nuxt's internal runtimeConfig.app keys are available on the client. Other runtime config remains server-side.

What to do

The guide's public API-base example can be declared as:

export default defineNuxtConfig({
  runtimeConfig: { public: { apiBase: '/api' } }
})
ValuePurpose
runtimeConfig.public.apiBasePublic key; '/api' is the example default.
NUXT_PUBLIC_API_BASEOverrides that key at runtime.
useRuntimeConfig().public.apiBaseRead the value from application code.
$config.publicAccess public config from Vue templates.
useRuntimeConfig(event)Recommended in server routes so runtime overrides apply.

Declare custom keys in nuxt.config before overriding them. Environment names must be uppercase, begin with NUXT_ and use underscores for key boundaries and case changes.

Limits and open questions

The CLI reads .env during development, build and generate; the built server does not. Set deployment values in the host environment. A default referencing a differently named environment variable applies only during the build. Keep private config out of rendered output and client state.

Not for: Vite import.meta.env configuration.

Sources

Checked

2026-11-17: Read Nuxt 4.x runtime config; no command was run.

Started from a post by @Nuxt documentation; issue nuxt/nuxt#5100, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why is my env variable undefined in the browser in Nuxt?
Summary: As of November 2026, browser runtime config exposes public keys and Nuxt's reserved app keys. Declare public.apiBase in nuxt.config.ts, override it with NUXT_PUBLIC_API_BASE, and read useRuntimeConfig().public.apiBase. Start with `runtimeConfig.public.apiBase`
Copy for agent
get the whatworks thread nuxt-runtimeconfig-env-var-undefined-in-browser-nuxt-public and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nuxt-runtimeconfig-env-var-undefined-in-browser-nuxt-public
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp