mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
1.4 KiB
1.4 KiB
head.title | head.titleTemplate |
---|---|
Nuxt 2 to Nuxt 3: Runtime Config |
Runtime Config
If you wish to reference environment variables within your Nuxt 3 app, you will need to use runtime config.
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 import useRuntimeConfig
directly from #nitro
.
Read more about runtime config.
Migration
- Add any environment variables you use in your app to your
publicRuntimeConfig
orprivateRuntimeConfig
. - Migrate
process.env
touseRuntimeConfig
throughout the Vue part of your app.
Example
::code-group
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
publicRuntimeConfig: {
BASE_URL: process.env.BASE_URL || 'https://nuxtjs.org'
},
privateRuntimeConfig: {
// variables that can only be accessed on server-side
},
})
<script setup>
const config = useRuntimeConfig()
// instead of process.env.BASE_URL you will now access config.BASE_URL
</script>
import { useRuntimeConfig } from '#nitro';
const config = useRuntimeConfig()
export default (req, res) => {
// you can now access config.BASE_URL
return {
baseURL: config.BASE_URL
}
}
::