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.
navigation.icon: i-ph-file-duotone
2022-09-13 12:54:31 +00:00
---
2022-08-17 15:23:13 +00:00
Nuxt 3 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-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.
::
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'
}
})
```
2023-10-18 10:59:43 +00:00
When adding `theme` to the `app.config` , Nuxt uses Vite or webpack to bundle the code. We can 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
```
2023-11-16 14:08:10 +00:00
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
## 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'],
})
```
::