mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-15 02:14:44 +00:00
67 lines
1.9 KiB
Markdown
67 lines
1.9 KiB
Markdown
---
|
|
title: 'modules'
|
|
head.title: 'modules/'
|
|
description: Use the modules/ directory to automatically register local modules within your application.
|
|
navigation.icon: i-ph-folder
|
|
---
|
|
|
|
It is a good place to place any local modules you develop while building your application.
|
|
|
|
The auto-registered files patterns are:
|
|
- `modules/*/index.ts`
|
|
- `modules/*.ts`
|
|
|
|
You don't need to add those local modules to your [`nuxt.config.ts`](/docs/guide/directory-structure/nuxt-config) separately.
|
|
|
|
::code-group
|
|
|
|
```ts twoslash [modules/hello/index.ts]
|
|
// `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')
|
|
})
|
|
}
|
|
})
|
|
```
|
|
|
|
```ts twoslash [modules/hello/runtime/api-route.ts]
|
|
export default defineEventHandler(() => {
|
|
return { hello: 'world' }
|
|
})
|
|
```
|
|
|
|
::
|
|
|
|
When starting Nuxt, the `hello` module will be registered and the `/api/hello` route will be available.
|
|
|
|
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:
|
|
|
|
```bash [Directory structure]
|
|
modules/
|
|
1.first-module/
|
|
index.ts
|
|
2.second-module.ts
|
|
```
|
|
|
|
:read-more{to="/docs/guide/going-further/modules"}
|
|
|
|
::tip{icon="i-ph-video" to="https://vueschool.io/lessons/creating-your-first-module-from-scratch?friend=nuxt" target="_blank"}
|
|
Watch Vue School video about Nuxt private modules.
|
|
::
|