2024-12-11 12:29:52 +00:00
---
title: 'shared'
head.title: 'shared/'
description: 'Use the shared/ directory to share functionality between the Vue app and the Nitro server.'
navigation.icon: 'i-ph-folder'
---
The `shared/` directory allows you to share code that can be used in both the Vue app and the Nitro server.
::note
The `shared/` directory is available in Nuxt v3.14+.
::
::important
Code in the `shared/` directory cannot import any Vue or Nitro code.
::
2025-02-08 21:22:17 +00:00
::warning
Auto-imports are not enabled by default in Nuxt v3 to prevent breaking changes in existing projects.
To use these auto-imported utils and types, you must first [set `future.compatibilityVersion: 4` in your `nuxt.config.ts` ](/docs/getting-started/upgrade#opting-in-to-nuxt-4 ).
::
2024-12-11 12:29:52 +00:00
## Usage
2025-02-08 21:22:17 +00:00
**Method 1:** Named export
2024-12-11 12:29:52 +00:00
```ts twoslash [shared/utils/capitalize.ts]
export const capitalize = (input: string) => {
2024-12-11 13:30:12 +00:00
return input[0] ? input[0].toUpperCase() + input.slice(1) : ''
2024-12-11 12:29:52 +00:00
}
```
2025-02-08 21:22:17 +00:00
**Method 2:** Default export
2024-12-11 12:29:52 +00:00
```ts twoslash [shared/utils/capitalize.ts]
2025-02-08 21:22:17 +00:00
export default function (input: string) {
2024-12-11 13:30:12 +00:00
return input[0] ? input[0].toUpperCase() + input.slice(1) : ''
2024-12-11 12:29:52 +00:00
}
```
2025-02-08 21:22:17 +00:00
You can now use [auto-imported ](/docs/guide/directory-structure/shared#auto-imports ) utilities in your Nuxt app and `server/` directory.
2024-12-11 12:29:52 +00:00
```vue [app.vue]
< script setup lang = "ts" >
const hello = capitalize('hello')
< / script >
< template >
< div >
{{ hello }}
< / div >
< / template >
```
```ts [server/api/hello.get.ts]
export default defineEventHandler((event) => {
return {
hello: capitalize('hello')
}
})
```
2025-02-08 21:22:17 +00:00
## How Files Are Scanned
2024-12-11 12:29:52 +00:00
2025-02-08 21:22:17 +00:00
Only files in the `shared/utils/` and `shared/types/` directories will be auto-imported. Files nested within subdirectories of these directories will not be auto-imported unless you add these directories to `imports.dirs` and `nitro.imports.dirs` .
2024-12-11 12:29:52 +00:00
::tip
The way `shared/utils` and `shared/types` auto-imports work and are scanned is identical to the [`composables/` ](/docs/guide/directory-structure/composables ) and [`utils/` ](/docs/guide/directory-structure/utils ) directories.
::
:read-more{to="/docs/guide/directory-structure/composables#how-files-are-scanned"}
```bash [Directory Structure]
-| shared/
---| capitalize.ts # Not auto-imported
---| formatters
-----| lower.ts # Not auto-imported
---| utils/
-----| lower.ts # Auto-imported
-----| formatters
-------| upper.ts # Not auto-imported
---| types/
-----| bar.d.ts # Auto-imported
```
Any other files you create in the `shared/` folder must be manually imported using the `#shared` alias (automatically configured by Nuxt):
```ts
// For files directly in the shared directory
import capitalize from '#shared/capitalize'
// For files in nested directories
import lower from '#shared/formatters/lower'
// For files nested in a folder within utils
import upper from '#shared/utils/formatters/upper'
```
This alias ensures consistent imports across your application, regardless of the importing file's location.
:read-more{to="/docs/guide/concepts/auto-imports"}