2022-11-09 09:43:16 +00:00
---
title: 'utils'
2022-11-21 15:51:39 +00:00
head.title: 'utils/'
2022-11-09 09:43:16 +00:00
description: Use the utils/ directory to auto-import your utility functions throughout your application.
2023-10-18 10:59:43 +00:00
navigation.icon: i-ph-folder-duotone
2022-11-09 09:43:16 +00:00
---
2023-10-18 10:59:43 +00:00
The main purpose of the [`utils/` directory ](/docs/guide/directory-structure/utils ) is to allow a semantic distinction between your Vue composables and other auto-imported utility functions.
2022-11-09 09:43:16 +00:00
2023-10-18 10:59:43 +00:00
## Usage
2022-11-09 09:43:16 +00:00
2023-10-18 10:59:43 +00:00
**Method 1:** Using named export
2024-02-16 20:31:15 +00:00
```ts twoslash [utils/index.ts]
2023-10-18 10:59:43 +00:00
export const { format: formatNumber } = Intl.NumberFormat('en-GB', {
notation: 'compact',
maximumFractionDigits: 1
})
```
**Method 2:** Using default export
2024-02-16 20:31:15 +00:00
```ts twoslash [utils/random-entry.ts or utils/randomEntry.ts]
2023-10-18 10:59:43 +00:00
// It will be available as randomEntry() (camelCase of file name without extension)
export default function (arr: Array< any > ) {
return arr[Math.floor(Math.random() * arr.length)]
}
```
You can now use auto imported utility functions in `.js` , `.ts` and `.vue` files
```vue [app.vue]
< template >
< p > {{ formatNumber(1234) }}< / p >
< / template >
```
:read-more{to="/docs/guide/concepts/auto-imports"}
:link-example{to="/docs/examples/features/auto-imports"}
2022-11-09 09:43:16 +00:00
2024-02-21 17:09:45 +00:00
::tip
2023-10-18 10:59:43 +00:00
The way `utils/` auto-imports work and are scanned is identical to the [`composables/` ](/docs/guide/directory-structure/composables ) directory.
2022-11-09 09:43:16 +00:00
::
2023-03-07 09:45:07 +00:00
2024-02-21 17:09:45 +00:00
::important
2023-10-18 10:59:43 +00:00
These utils are only available within the Vue part of your app. :br
Only `server/utils` are auto-imported in the [`server/` ](/docs/guide/directory-structure/server#server-utilities ) directory.
2023-03-07 09:45:07 +00:00
::