nuxt.config.ts - runtimeconfig - .env
In my Nuxt config file, when i want to leave the config empty and override it using my .env file, it works only on localhost, and does not work on my VPS when i build it.
NUXT_PUBLIC_API_BASE


2 Replies
Hi @Rekar Omar. 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: .env is loaded automatically, and variables like NUXT_PUBLIC_API_BASE override your runtimeConfig defaults.
- Production (after build): .env is 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:
In your .env (used only for dev/build):
On your VPS, you must set the environment variable before starting the server:
or use your process manager (like pm2, systemd, etc.) to set the variable.
Key points:
- .env is ignored in production runtime.
- Use real environment variables in production.
- Only variables prefixed with NUXT_ will override runtimeConfig in production.
For more details, see the official docs: Nuxt 4.x Guide: .env, Runtime Config
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1368120799374082150> Kapa.ai is still learning and improving, please let me know how I did by reacting below.