#nuxt.config.ts - runtimeconfig - .env
3 messages · Page 1 of 1 (latest)
Hi @pastel cloud. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
This behavior is expected in Nuxt 3 and 4. The .env file is only used during development, build, and generate time. When you run your built app in production (such as on your VPS), Nuxt does not read the .env file. Instead, you must set environment variables directly in your production environment.
How it works:
- Development/Build:
.envis loaded automatically, and variables likeNUXT_PUBLIC_API_BASEoverride yourruntimeConfigdefaults. - Production (after build):
.envis not loaded. You must set environment variables (e.g.,NUXT_PUBLIC_API_BASE) using your VPS or hosting provider's environment configuration.
Example:
In your nuxt.config.ts:
export default defineNuxtConfig({
runtimeConfig: {
public: {
apiBase: '', // Will be overridden by NUXT_PUBLIC_API_BASE
},
},
})
In your .env (used only for dev/build):
NUXT_PUBLIC_API_BASE=<https://api.example.com>
On your VPS, you must set the environment variable before starting the server:
NUXT_PUBLIC_API_BASE=<https://api.example.com> node .output/server/index.mjs
or use your process manager (like pm2, systemd, etc.) to set the variable.
Key points:
.envis ignored in production runtime.- Use real environment variables in production.
- Only variables prefixed with
NUXT_will overrideruntimeConfigin production.