It is a good place to place any local modules you develop while building your application.
The auto-registered files patterns are:
modules/*/index.tsmodules/*.tsYou don't need to add those local modules to your nuxt.config.ts separately.
// `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 { addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit'
export default defineNuxtModule({
meta: {
name: 'hello',
},
setup () {
const resolver = createResolver(import.meta.url)
// Add an API route
addServerHandler({
route: '/api/hello',
handler: resolver.resolve('./runtime/api-route'),
})
},
})
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:
nuxt.config.ts are loaded.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:
modules/
1.first-module/
index.ts
2.second-module.ts