--- title: "Module Author Guide" description: "Learn how to create a Nuxt module." --- # Module Author Guide A powerful configuration and hooks system makes it possible to customize almost every aspect of Nuxt Framework and add endless possible integrations when it comes to customization. Nuxt provides a zero-config experience with a preset of integrations and best practices to develop Web applications. A powerful configuration and hooks system makes it possible to customize almost every aspect of Nuxt Framework and add endless possible integrations when it comes to customization. You can learn more about how Nuxt works in the [Nuxt internals](/docs/guide/going-further/internals) section. Nuxt exposes a powerful API called **Nuxt Modules**. Nuxt modules are simple async functions that sequentially run when starting Nuxt in development mode using `nuxi dev` or building a project for production with `nuxi build`. Using Nuxt Modules, we can encapsulate, properly test and share custom solutions as npm packages without adding unnecessary boilerplate to the Nuxt project itself. Nuxt Modules can hook into lifecycle events of Nuxt builder, provide runtime app templates, update the configuration or do any other custom action based on needs. ## Quick Start For the impatient ones, You can quickly start with [module-builder](https://github.com/nuxt/module-builder) and [module starter template](https://github.com/nuxt/starter/tree/module): ```bash npx nuxi init -t module my-module ``` Starter template and module starter is a standard path of creating a Nuxt module. **Next steps:** 1. Open `my-module` in the IDE of your choice (Visual Studio Code is recommended) 2. Install dependencies using the package manager of your choice (Yarn is recommended) 3. Ensure local files are generated using `npm run dev:prepare` 4. Start playground using `npm run dev` 5. Follow this document to learn more about Nuxt modules ::alert{type=info icon=🚧} This is an under-the-progress guide. Please regularly check for updates. :: ## Module Anatomy A Nuxt module is a simple function accepting inline user options and `nuxt` arguments. It is totally up to you, as the module author, how to handle the rest of the logic. Starting with Nuxt 3, modules can benefit all [Nuxt Kit](/docs/api/advanced/kit) utilities. ```ts [modules/example.ts] // modules/module.mjs export default async (inlineOptions, nuxt) => { // You can do whatever you like here.. console.log(inlineOptions.token) // `123` console.log(nuxt.options.dev) // `true` or `false` nuxt.hook('ready', async nuxt => { console.log('Nuxt is ready') }) } ``` ```ts [nuxt.config] export default defineNuxtConfig({ modules: [ // Using package name (recommended usage) '@nuxtjs/example', // Load a local module './modules/example', // Add module with inline-options ['./modules/example', { token: '123' }] // Inline module definition async (inlineOptions, nuxt) => { } ] }) ``` ## Defining Nuxt Modules Creating Nuxt modules involves tedious and common tasks. [Nuxt Kit](/docs/api/advanced/kit), provides a convenient and standard API to define Nuxt modules using `defineNuxtModule`: ```js import { defineNuxtModule } from '@nuxt/kit' export default defineNuxtModule({ meta: { // Usually npm package name of your module name: '@nuxtjs/example', // The key in `nuxt.config` that holds your module options configKey: 'sample', // Compatibility constraints compatibility: { // Semver version of supported nuxt versions nuxt: '^3.0.0' } }, // Default configuration options for your module defaults: {}, hooks: {}, async setup(moduleOptions, nuxt) { // -- Add your module logic here -- } }) ``` The result of `defineNuxtModule` is a wrapper function with an `(inlineOptions, nuxt)` signature. It applies defaults and other necessary steps and calls the `setup` function when called. **`defineNuxtModule` features:** ::list - Support `defaults` and `meta.configKey` for automatically merging module options - Type hints and automated type inference - Add shims for basic Nuxt 2 compatibility - Ensure module gets installed only once using a unique key computed from `meta.name` or `meta.configKey` - Automatically register Nuxt hooks - Automatically check for compatibility issues based on module meta - Expose `getOptions` and `getMeta` for internal usage of Nuxt - Ensuring backward and upward compatibility as long as the module is using `defineNuxtModule` from the latest version of `@nuxt/kit` - Integration with module builder tooling :: ## Best Practices ### Async Modules Nuxt Modules can do asynchronous operations. For example, you may want to develop a module that needs fetching some API or calling an async function. ::alert{type="warning"} Be careful that `nuxi dev` waits for your module setup before going to the next module and starting the development server. Do time-consuming logic using deferred Nuxt hooks. :: ### Always Prefix Exposed Interfaces Nuxt Modules should provide an explicit prefix for any exposed configuration, plugin, API, composable, or component to avoid conflict with other modules and internals. Ideally, you should prefix them with your module's name (e.g. if your module is called `nuxt-foo`, expose `` and `useFooBar()` and **not** `