2022-09-13 12:54:31 +00:00
---
title: app.config.ts
2022-11-21 15:51:39 +00:00
head.title: 'app.config.ts'
2023-10-18 10:59:43 +00:00
description: Expose reactive configuration within your application with the App Config file.
2024-09-17 15:33:49 +00:00
navigation.icon: i-ph-file
2022-09-13 12:54:31 +00:00
---
2024-06-30 10:11:16 +00:00
Nuxt provides an `app.config` config file to expose reactive configuration within your application with the ability to update it at runtime within lifecycle or using a nuxt plugin and editing it with HMR (hot-module-replacement).
2022-08-17 15:23:13 +00:00
2022-09-13 12:54:31 +00:00
You can easily provide runtime app configuration using `app.config.ts` file. It can have either of `.ts` , `.js` , or `.mjs` extensions.
2024-02-16 20:31:15 +00:00
```ts twoslash [app.config.ts]
2022-09-13 12:54:31 +00:00
export default defineAppConfig({
foo: 'bar'
})
```
2024-02-21 17:09:45 +00:00
::caution
2022-08-17 15:23:13 +00:00
Do not put any secret values inside `app.config` file. It is exposed to the user client bundle.
::
2024-10-12 20:31:58 +00:00
::note
When configuring a custom [`srcDir` ](/docs/api/nuxt-config#srcdir ), make sure to place the `app.config` file at the root of the new `srcDir` path.
::
2023-10-18 10:59:43 +00:00
## Usage
2022-08-17 15:23:13 +00:00
To expose config and environment variables to the rest of your app, you will need to define configuration in `app.config` file.
2024-02-16 20:31:15 +00:00
```ts twoslash [app.config.ts]
2022-08-17 15:23:13 +00:00
export default defineAppConfig({
theme: {
primaryColor: '#ababab'
}
})
```
2024-10-12 20:31:58 +00:00
We can now universally access `theme` both when server-rendering the page and in the browser using [`useAppConfig` ](/docs/api/composables/use-app-config ) composable.
2023-02-22 05:50:14 +00:00
2023-10-18 10:59:43 +00:00
```vue [pages/index.vue]
2024-02-13 12:50:38 +00:00
< script setup lang = "ts" >
2022-08-17 15:23:13 +00:00
const appConfig = useAppConfig()
console.log(appConfig.theme)
2023-10-18 10:59:43 +00:00
< / script >
2022-08-17 15:23:13 +00:00
```
2024-10-12 20:31:58 +00:00
The [`updateAppConfig` ](/docs/api/utils/update-app-config ) utility can be used to update the `app.config` at runtime.
```vue [pages/index.vue]
< script setup >
const appConfig = useAppConfig() // { foo: 'bar' }
const newAppConfig = { foo: 'baz' }
updateAppConfig(newAppConfig)
console.log(appConfig) // { foo: 'baz' }
< / script >
```
::read-more{to="/docs/api/utils/update-app-config"}
Read more about the `updateAppConfig` utility.
::
2023-11-16 14:08:10 +00:00
2023-10-18 10:59:43 +00:00
## Typing App Config
2022-08-17 15:23:13 +00:00
2023-10-18 10:59:43 +00:00
Nuxt tries to automatically generate a TypeScript interface from provided app config so you won't have to type it yourself.
2023-02-04 14:29:33 +00:00
2023-10-18 10:59:43 +00:00
However, there are some cases where you might want to type it yourself. There are two possible things you might want to type.
2023-02-04 14:29:33 +00:00
2023-10-18 10:59:43 +00:00
### App Config Input
2022-08-17 15:23:13 +00:00
2023-02-04 14:29:33 +00:00
`AppConfigInput` might be used by module authors who are declaring what valid _input_ options are when setting app config. This will not affect the type of `useAppConfig()` .
2022-08-17 15:23:13 +00:00
```ts [index.d.ts]
2023-02-13 22:42:04 +00:00
declare module 'nuxt/schema' {
2022-09-06 16:15:27 +00:00
interface AppConfigInput {
2022-08-17 15:23:13 +00:00
/** Theme configuration */
2022-09-06 16:15:27 +00:00
theme?: {
2022-08-17 15:23:13 +00:00
/** Primary app color */
2022-09-06 16:15:27 +00:00
primaryColor?: string
2022-08-17 15:23:13 +00:00
}
}
}
// It is always important to ensure you import/export something when augmenting a type
export {}
```
2023-02-04 14:29:33 +00:00
2023-10-18 10:59:43 +00:00
### App Config Output
2023-02-04 14:29:33 +00:00
2023-10-18 10:59:43 +00:00
If you want to type the result of calling [`useAppConfig()` ](/docs/api/composables/use-app-config ), then you will want to extend `AppConfig` .
2023-02-04 14:29:33 +00:00
2024-02-21 17:09:45 +00:00
::warning
2023-02-04 14:29:33 +00:00
Be careful when typing `AppConfig` as you will overwrite the types Nuxt infers from your actually defined app config.
::
```ts [index.d.ts]
2023-02-13 22:42:04 +00:00
declare module 'nuxt/schema' {
2023-02-04 14:29:33 +00:00
interface AppConfig {
// This will entirely replace the existing inferred `theme` property
theme: {
// You might want to type this value to add more specific types than Nuxt can infer,
// such as string literal types
primaryColor?: 'red' | 'blue'
}
}
}
// It is always important to ensure you import/export something when augmenting a type
export {}
```
2023-06-09 09:28:16 +00:00
2023-10-18 10:59:43 +00:00
## Merging Strategy
Nuxt uses a custom merging strategy for the `AppConfig` within [the layers ](/docs/getting-started/layers ) of your application.
2023-06-09 09:28:16 +00:00
This strategy is implemented using a [Function Merger ](https://github.com/unjs/defu#function-merger ), which allows defining a custom merging strategy for every key in `app.config` that has an array as value.
2024-02-21 17:09:45 +00:00
::note
2024-03-30 07:43:54 +00:00
The function merger can only be used in the extended layers and not the main `app.config` in project.
2023-06-09 09:28:16 +00:00
::
Here's an example of how you can use:
::code-group
2024-02-16 20:31:15 +00:00
```ts twoslash [layer/app.config.ts]
2023-06-09 09:28:16 +00:00
export default defineAppConfig({
// Default array value
array: ['hello'],
})
```
2024-02-16 20:31:15 +00:00
```ts twoslash [app.config.ts]
2023-06-09 09:28:16 +00:00
export default defineAppConfig({
// Overwrite default array value by using a merger function
array: () => ['bonjour'],
})
```
::
2024-09-07 17:22:46 +00:00
## Known Limitations
As of Nuxt v3.3, the `app.config.ts` file is shared with Nitro, which results in the following limitations:
1. You cannot import Vue components directly in `app.config.ts` .
2. Some auto-imports are not available in the Nitro context.
These limitations occur because Nitro processes the app config without full Vue component support.
While it's possible to use Vite plugins in the Nitro config as a workaround, this approach is not recommended:
```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
vite: {
plugins: [vue()]
}
}
})
```
::warning
Using this workaround may lead to unexpected behavior and bugs. The Vue plugin is one of many that are not available in the Nitro context.
::
Related issues:
- [Issue #19858 ](https://github.com/nuxt/nuxt/issues/19858 )
- [Issue #19854 ](https://github.com/nuxt/nuxt/issues/19854 )
::info
Nitro v3 will resolve these limitations by removing support for the app config.
You can track the progress in [this pull request ](https://github.com/unjs/nitro/pull/2521 ).
: :