Modify Nuxt Configuration
Nuxt configuration can be read and altered by modules. Here’s an example of a module enabling an experimental feature.Expose Options to Runtime
Because modules aren’t part of the application runtime, their options aren’t either. However, in many cases, you might need access to some of these module options within your runtime code. We recommend exposing the needed config using Nuxt’sruntimeConfig.
defu to extend the public runtime configuration the user provides instead of overwriting it.
You can then access your module options in a plugin, component, the application like any other runtime configuration:
Add Plugins
Plugins are a common way for a module to add runtime logic. You can use theaddPlugin utility to register them from your module.
Add Components
If your module should provide Vue components, you can use theaddComponent utility to add them as auto-imports for Nuxt to resolve.
addComponentsDir.
Note that all components, pages, composables and other files that would be normally placed in your
app/ folder need to be in runtime/app/. This will mean they can be type checked properly.Add Composables
If your module should provide composables, you can use theaddImports utility to add them as auto-imports for Nuxt to resolve.
addImportsDir.
Add Keyed Functions
Sometimes, you may need to maintain state consistency between the server and the client. Examples include Nuxt’s built-inuseState or useAsyncData composables.
When a function is registered, Nuxt’s compiler automatically injects a unique key as an additional argument if the function is called with fewer than the specified number of arguments. This key remains stable between server-side rendering and client hydration.
Use the keyedComposables option to register your function:
keyedComposables configuration accepts an array of objects with the following properties:
For example, with
argumentLength: 2:
Add Server Routes
You can add server routes to your module using theaddServerHandler utility.
Add Other Assets
If your module should provide other kinds of assets, they can also be injected. Here’s a simple example module injecting a stylesheet through Nuxt’scss array.
publicAssets option:
Use Other Modules
If your module depends on other modules, you can specify them using themoduleDependencies option. This provides a more robust way to handle module dependencies with version constraints and configuration merging:
Use Lifecycle Hooks
Lifecycle hooks allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through thehooks map in their definition.
Module Cleanup
If your module opens, handles, or starts a watcher, you should close it when the Nuxt lifecycle is done. Theclose hook is available for this.
Add Virtual Files
If you need to add a virtual file that can be imported into the user’s app, you can use theaddTemplate utility.
addServerTemplate utility instead.
Add Type Declarations
You might also want to add a type declaration to the user’s project (for example, to augment a Nuxt interface or provide a global type of your own). For this, Nuxt provides theaddTypeTemplate utility that both writes a template to the disk and adds a reference to it in the generated nuxt.d.ts file.
Next Steps
Now that you’ve learned the common module recipes, explore best practices for building production-ready modules.Best Practices
Learn how to build performant and maintainable modules