nuxthub How to use separated env for local/stage/live
As now I have runtime config
But during deploy it reads all things from my local .env file, it skips the envs set in nuxthub. I understand usage for
NUXT_PUBLIC_ but how to work with live/stage/local in that case?
What prefix to use for non public ?3 Replies
Hi @Isu. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To use separate environment variables for local, staging, and live environments in Nuxt, you should:
1. Use different
- Use
.env files for each environment (e.g., .env.local, .env.staging, .env.production). When running locally, Nuxt will load the .env file automatically. For other environments, you can specify which file to use with the --dotenv flag, e.g.:
However, in production, Nuxt does not read .env files—you must set environment variables using your hosting platform's configuration (e.g., dashboard, CLI, or shell) Docs: .env.
2. Prefix environment variables for public and private use:
- For variables you want to expose to the client, use the NUXT_PUBLIC_ prefix (e.g., NUXT_PUBLIC_API_BASE). These go under runtimeConfig.public.
- For private variables (server-only), use the NUXT_ prefix (e.g., NUXT_LYS_WEARHOUSE_KEY). These go at the root of runtimeConfig (not under public) Docs: runtimeConfig.
3. How Nuxt resolves environment variables:
- At runtime, Nuxt will look for environment variables matching your runtimeConfig keys, using the NUXT_ or NUXT_PUBLIC_ prefix.
- For example, to override backendKey, set NUXT_LYS_WEARHOUSE_KEY in your environment. For public.apiBase, set NUXT_PUBLIC_API_BASE Docs: runtimeConfig.
4. Production/Stage/Local separation:
- In production, set environment variables using your deployment platform (e.g., Vercel, Netlify, Heroku, Docker environment: block, etc.). Do not rely on .env files in production—they are not loaded Docs: .env Production.
- For staging, do the same: set the appropriate environment variables for that environment.
- Locally, use .env files as needed.
Example:
And in your nuxt.config.ts:
Summary:- Use
NUXT_ for private (server-only) variables, and NUXT_PUBLIC_ for public (client-exposed) variables.
- In production/staging, set environment variables via your platform, not .env files.
- Use the --dotenv flag for custom .env files in local/dev.
- Do not use process.env.* directly in your app code; use useRuntimeConfig() instead.
For more details, see the official docs on runtime config and environment variables.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@219508202830561281> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Thing is I used NUXT_ and I added new ones in nuxthub, but seems like it reads the ones in my local .env and ignored the one I added in nuxthub - basically it just reads it during build time and deploy when I use npx nuxthub deploy @kapa.ai