2022-04-06 05:56:08 +00:00
---
head.title: 'Nuxt 2 to Nuxt 3: Runtime Config'
head.titleTemplate: ''
---
2022-03-30 17:32:30 +00:00
# Runtime Config
If you wish to reference environment variables within your Nuxt 3 app, you will need to use runtime config.
2022-04-19 19:10:32 +00:00
When referencing these variables within your components, you will have to use the `useRuntimeConfig` composable in your setup method (or Nuxt plugin). In the `server/` portion of your app, you can use `useRuntimeConfig` without any import.
2022-03-30 17:32:30 +00:00
2022-04-06 05:56:08 +00:00
[Read more about runtime config ](/guide/features/runtime-config ).
2022-03-30 17:32:30 +00:00
## Migration
1. Add any environment variables you use in your app to your `publicRuntimeConfig` or `privateRuntimeConfig` .
1. Migrate `process.env` to `useRuntimeConfig` throughout the Vue part of your app.
## Example
::code-group
```ts [nuxt.config.ts]
2022-04-20 08:52:39 +00:00
import { defineNuxtConfig } from 'nuxt'
2022-03-30 17:32:30 +00:00
export default defineNuxtConfig({
2022-04-12 08:12:33 +00:00
runtimeConfig: {
secretKey: '', // variables that can only be accessed on server-side
public: {
BASE_URL: process.env.BASE_URL || 'https://nuxtjs.org'
}
2022-03-30 17:32:30 +00:00
},
})
```
```vue [pages/index.vue]
< script setup >
2022-04-12 08:12:33 +00:00
const config = useRuntimeConfig().public
2022-03-30 17:32:30 +00:00
// instead of process.env.BASE_URL you will now access config.BASE_URL
< / script >
```
```ts [server/api/hello.ts]
2022-04-12 08:12:33 +00:00
const config = useRuntimeConfig().public
2022-04-07 13:47:15 +00:00
2022-03-30 17:32:30 +00:00
export default (req, res) => {
// you can now access config.BASE_URL
return {
baseURL: config.BASE_URL
}
}
```
::