| title | Module Builder | ||
|---|---|---|---|
| description | Build and publish Nuxt modules with `@nuxt/module-builder`. | ||
| navigation |
|
@nuxt/module-builder builds a Nuxt module for publishing to npm. It provides a tsdown plugin, builder(), that bundles your module entry and transpiles your runtime directory file by file. It also generates type declarations against your module's own .nuxt, and writes the types that give nuxt.config autocompletion for your module's options.
@nuxt/module-builder works with the layout of the module starter and needs no options for it. To learn how to write the module itself, such as its definition, its hooks, and the @nuxt/kit utilities, read the Module Author Guide.
To build a module, you need:
- Node
^22.18.0 || ^24.11.0 || >=26.0.0. tsdownandtypescript(^5.9.3 || ^6.0.0), which are peer dependencies of@nuxt/module-builder.nuxt, which provides the@nuxt/clithatnuxt-module-build prepareruns.
An app needs Nuxt 3 or later to use a module that @nuxt/module-builder builds. The module entry is compiled for the lowest Node version in your module's engines.node field. Without that field, its syntax is kept as written.
::note Nuxt 3 has reached end of life, so these docs describe Nuxt 4 and later. They don't cover the places where Nuxt 3 behaves differently, such as type-checking all of a module's runtime files in a single context. ::
Create a module from the starter:
pnpm create nuxt -t module my-moduleThe starter uses source exports with pnpm, so create nuxt installs its dependencies with pnpm, whichever package manager runs it. pnpm has to be installed.
Then start the playground:
cd my-module
pnpm devdev runs nuxt-module-build prepare, which generates the .nuxt that types your module, and then nuxt dev playground. To generate the types of the module and the playground without starting the playground, such as before a type check, run pnpm dev:prepare. To find out what each script does, or to develop with another package manager, read Develop and Publish.
To add @nuxt/module-builder to an existing module, follow these steps:
-
Install
@nuxt/module-builderand its peer dependencies:pnpm add -D --ignore-workspace-root-check @nuxt/module-builder@^2.0.0-alpha.0 tsdown@^0.23.7 typescript nuxt
--ignore-workspace-root-checkletspnpm addinstall at the root of a pnpm workspace, and has no effect in a single package. If pnpm'sminimumReleaseAgeor Yarn'snpmMinimalAgeGaterejects a recent release, see Install v2 and tsdown. -
Create a
tsdown.config.tsin the module root that uses thebuilder()plugin:import { builder } from '@nuxt/module-builder/tsdown' import { defineConfig } from 'tsdown' export default defineConfig({ plugins: [builder()], })
-
Add the scripts that prepare your module for development and build it before publishing:
{ "scripts": { "dev": "nuxt-module-build prepare && nuxt dev playground", "dev:prepare": "nuxt-module-build prepare && nuxt prepare playground", "prepack": "tsdown" } } -
Point the
exportsofpackage.jsonat your module, as described in Project Structure. -
Create a
tsconfig.jsonthat references the tsconfigs in.nuxt, as described in TypeScript.
The dev:prepare script in step 3 is for a module that you publish with pnpm or Yarn 4. If you publish with npm, Yarn 3 or earlier, or bun, add tsdown before nuxt prepare playground. Develop and Publish explains the difference.
- Project Structure: the files that
@nuxt/module-builderreads. - Commands:
tsdown,nuxt-module-build build, andnuxt-module-build prepare. - Develop and Publish: the two development setups, the playground, and releases.
- Configuration:
tsdown.config.ts, extra entries, and externals. - TypeScript: your module's
tsconfig.json, its declarations, and the rules for module code. - Build Output: the files in
dist, and the size report. - Exports: subpath exports, including runtime components.
- Upgrading to v2: moving a module from v1.