2023-03-03 17:52:55 +00:00
|
|
|
---
|
|
|
|
title: 'modules'
|
|
|
|
head.title: 'modules/'
|
|
|
|
description: Use the modules/ directory to automatically register local modules within your application.
|
2024-09-17 15:33:49 +00:00
|
|
|
navigation.icon: i-ph-folder
|
2023-03-03 17:52:55 +00:00
|
|
|
---
|
|
|
|
|
2023-10-18 10:59:43 +00:00
|
|
|
It is a good place to place any local modules you develop while building your application.
|
2023-03-03 17:52:55 +00:00
|
|
|
|
|
|
|
The auto-registered files patterns are:
|
|
|
|
- `modules/*/index.ts`
|
|
|
|
- `modules/*.ts`
|
|
|
|
|
2023-10-25 09:19:09 +00:00
|
|
|
You don't need to add those local modules to your [`nuxt.config.ts`](/docs/guide/directory-structure/nuxt-config) separately.
|
2023-03-03 17:52:55 +00:00
|
|
|
|
|
|
|
::code-group
|
2023-04-04 13:23:13 +00:00
|
|
|
|
2024-02-16 20:31:15 +00:00
|
|
|
```ts twoslash [modules/hello/index.ts]
|
2023-03-03 17:52:55 +00:00
|
|
|
// `nuxt/kit` is a helper subpath import you can use when defining local modules
|
|
|
|
// that means you do not need to add `@nuxt/kit` to your project's dependencies
|
|
|
|
import { createResolver, defineNuxtModule, addServerHandler } from 'nuxt/kit'
|
|
|
|
|
|
|
|
export default defineNuxtModule({
|
|
|
|
meta: {
|
|
|
|
name: 'hello'
|
|
|
|
},
|
|
|
|
setup () {
|
|
|
|
const { resolve } = createResolver(import.meta.url)
|
|
|
|
|
|
|
|
// Add an API route
|
|
|
|
addServerHandler({
|
|
|
|
route: '/api/hello',
|
|
|
|
handler: resolve('./runtime/api-route')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
```
|
2023-04-04 13:23:13 +00:00
|
|
|
|
2024-02-16 20:31:15 +00:00
|
|
|
```ts twoslash [modules/hello/runtime/api-route.ts]
|
2023-03-03 17:52:55 +00:00
|
|
|
export default defineEventHandler(() => {
|
|
|
|
return { hello: 'world' }
|
2023-05-22 19:44:17 +00:00
|
|
|
})
|
2023-03-03 17:52:55 +00:00
|
|
|
```
|
2023-04-04 13:23:13 +00:00
|
|
|
|
2023-03-03 17:52:55 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
When starting Nuxt, the `hello` module will be registered and the `/api/hello` route will be available.
|
|
|
|
|
2024-05-13 20:59:33 +00:00
|
|
|
Modules are executed in the following sequence:
|
|
|
|
- First, the modules defined in [`nuxt.config.ts`](/docs/api/nuxt-config#modules-1) are loaded.
|
|
|
|
- Then, modules found in the `modules/` directory are executed, and they load in alphabetical order.
|
|
|
|
|
|
|
|
You can change the order of local module by adding a number to the front of each directory name:
|
2023-03-05 09:51:10 +00:00
|
|
|
|
2023-10-18 10:59:43 +00:00
|
|
|
```bash [Directory structure]
|
2023-03-05 09:51:10 +00:00
|
|
|
modules/
|
|
|
|
1.first-module/
|
|
|
|
index.ts
|
|
|
|
2.second-module.ts
|
2023-03-08 16:06:22 +00:00
|
|
|
```
|
2023-03-05 09:51:10 +00:00
|
|
|
|
2023-10-18 10:59:43 +00:00
|
|
|
:read-more{to="/docs/guide/going-further/modules"}
|
2024-03-11 16:56:57 +00:00
|
|
|
|
2024-09-17 15:33:49 +00:00
|
|
|
::tip{icon="i-ph-video" to="https://vueschool.io/lessons/creating-your-first-module-from-scratch?friend=nuxt" target="_blank"}
|
2024-03-11 16:56:57 +00:00
|
|
|
Watch Vue School video about Nuxt private modules.
|
|
|
|
::
|