Modules
Nuxt Kit provides a set of utilities to help you create and use modules. Modules are the building blocks of Nuxt, offering a structured way to keep your code organized and modular.defineNuxtModule
Define a Nuxt module, automatically merging defaults with user provided options, installing any hooks that are provided, and calling an optional setup function for full control.
Type
Parameters
definition: A module definition object with the following properties:| Property | Type | Required | Description |
|---|---|---|---|
meta | ModuleMeta | false | Metadata of the module. Defines the module name, version, config key, and compatibility. |
defaults | T | ((nuxt: Nuxt) => T) | false | Default options for the module. If a function is provided, it will be called with the Nuxt instance. |
schema | T | false | Schema for the module options. If provided, options will be applied to the schema. |
hooks | Partial<NuxtHooks> | false | Hooks to be installed for the module. |
moduleDependencies | Record<string, ModuleDependency> | ((nuxt: Nuxt) => Record<string, ModuleDependency>) | false | Dependencies on other modules with version constraints and configuration. |
onInstall | (nuxt: Nuxt) => Awaitable<void> | false | Lifecycle hook called when the module is first installed. Requires meta.name and meta.version. |
onUpgrade | (nuxt: Nuxt, options: T, previousVersion: string) => Awaitable<void> | false | Lifecycle hook called when the module is upgraded. Requires meta.name and meta.version. |
setup | (this: void, resolvedOptions: T, nuxt: Nuxt) => Awaitable<void | false | ModuleSetupInstallResult> | false | Setup function for the module. |
Usage
Using configKey to Make Your Module Configurable
When defining a Nuxt module, you can set a configKey to specify how users should configure the module in their nuxt.config.
Defining Module Compatibility Requirements
If you’re developing a Nuxt module and using APIs that are only supported in specific Nuxt versions, includecompatibility.nuxt.
Type Safety with .with()
When you need type safety for your resolved/merged module options, use the .with() method.
Lifecycle Hooks
You can define lifecycle hooks that run when your module is first installed or upgraded to a new version. Important: For lifecycle hooks to work, you must provide bothmeta.name and meta.version.
Specifying Module Dependencies
Use themoduleDependencies option to declare dependencies on other modules.