useRuntimeConfig composable in your setup method (or Nuxt plugin).
In the server/ portion of your app, you can use useRuntimeConfig without any import.
Why Runtime Config?
Runtime config provides:- Type safety - Full TypeScript support for your configuration
- Security - Separation of public and private configuration
- Environment variables - Automatic replacement at runtime
- Universal access - Available in both server and client code
Migration Steps
- Add environment variables to
runtimeConfigin yournuxt.configfile - Replace
process.envwithuseRuntimeConfigthroughout the Vue part of your app
Configuration Example
nuxt.config.ts
Client-side Usage
In your pages and components, use theuseRuntimeConfig composable:
app/pages/index.vue
Server-side Usage
In server routes and API handlers, you can access both public and private config:server/api/hello.ts
Environment Variables
Runtime config values are automatically replaced by matching environment variables at runtime:.env
Naming Convention
Environment variables follow this pattern:- Private config:
NUXT_<CONFIG_KEY>→runtimeConfig.configKey - Public config:
NUXT_PUBLIC_<CONFIG_KEY>→runtimeConfig.public.configKey
Before and After Comparison
Type Safety
You can define types for your runtime config:Best Practices
- Never expose secrets to the client - Only use
publicfor non-sensitive data - Provide defaults - Always set default values in
nuxt.config - Use environment variables - Override defaults with
.envfiles - Type your config - Add TypeScript interfaces for better DX