How does Nuxt.js handle environment variables?

Answer

Nuxt.js manages environment variables through runtime config defined in nuxt.config.ts. Public variables (safe to expose to the client) go under runtimeConfig.public, while private variables (server-only secrets) go under runtimeConfig directly. These are populated at runtime from your .env file — Nuxt automatically maps NUXT_PUBLIC_API_URL to runtimeConfig.public.apiUrl. On the client and server, you access them via const config = useRuntimeConfig(). This approach is safer than using import.meta.env directly, as it prevents accidentally leaking server secrets to the browser bundle.