| title | Project Structure |
|---|---|
| description | The files and directories that `@nuxt/module-builder` reads. |
@nuxt/module-builder builds a module with this layout:
my-module/
├── src/
│ ├── module.ts # the module definition
│ └── runtime/ # code added to the app that installs the module
│ ├── app/ # components, composables, plugins, and other app code
│ └── server/ # server routes, middleware, and Nitro plugins
├── playground/ # a Nuxt app that installs the module
├── package.json
├── tsconfig.json # references the tsconfigs in .nuxt
├── tsconfig.test.json # type-checks tests and config files
└── tsdown.config.ts # the build configurationThis page describes what each of these files and directories contains.
src/module.ts is the module entry. Its default export is the module definition, which you create with defineNuxtModule from @nuxt/kit. The build bundles it into dist/module.mjs.
The build also reads these type exports, and adds them to the types of the app that installs the module:
| Export | Augments | Used for |
|---|---|---|
ModuleOptions |
The module's key in nuxt.config. When you don't export it, it's inferred from the default export. |
|
ModuleHooks |
NuxtHooks from @nuxt/schema |
Build-time hooks the module calls, which apps and other modules register with hooks or nuxt.hook(). |
ModuleRuntimeHooks |
RuntimeNuxtHooks from #app |
Runtime hooks the module calls, which plugins register with nuxtApp.hook(). |
ModuleRuntimeConfig |
RuntimeConfig from @nuxt/schema |
Private runtime config keys the module adds. |
ModulePublicRuntimeConfig |
PublicRuntimeConfig from @nuxt/schema |
Public runtime config keys the module adds. |
import { addPlugin, createResolver, defineNuxtModule, updateRuntimeConfig } from '@nuxt/kit'
export interface ModuleOptions {
greeting: string
apiKey: string
}
export interface ModuleHooks {
'my-module:init': (options: ModuleOptions) => void
}
export interface ModuleRuntimeHooks {
'my-module:ready': () => void
}
export interface ModuleRuntimeConfig {
myModule: { apiKey: string }
}
export interface ModulePublicRuntimeConfig {
myModule: { greeting: string }
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'my-module',
configKey: 'myModule',
compatibility: { nuxt: '>=4.0.0' },
},
defaults: {
greeting: 'Hello',
apiKey: '',
},
setup(options) {
const resolver = createResolver(import.meta.url)
updateRuntimeConfig({
myModule: { apiKey: options.apiKey },
public: { myModule: { greeting: options.greeting } },
})
addPlugin(resolver.resolve('./runtime/app/plugins/greeting'))
},
})nuxt-module-build prepare and the build load src/module.ts with Node, together with every file that it imports by value. Node resolves imports as written, so relative imports in these files carry the imported file's extension:
import { defineNuxtModule } from '@nuxt/kit'
import { resolveOptions } from './options.ts'These files also use only the TypeScript syntax that Node's type stripping removes. Module code lists all the rules.
Write paths into the runtime directory without an extension, as in the addPlugin call above. The build emits src/runtime/app/plugins/greeting.ts as dist/runtime/app/plugins/greeting.js. Nuxt resolves a path without an extension to the file that exists: the .ts source when package.json points at your sources, and the .js output when it points at dist.
The runtime directory holds the code that the module adds to the app: components, composables, plugins, server routes, stylesheets, and any other files. The module registers them from setup(), with @nuxt/kit utilities such as addPlugin, addComponent, addImports, and addServerHandler.
Put code for the app in src/runtime/app/, and code for the server in src/runtime/server/. In an app that installs the module, Nuxt type-checks runtime/server/ in the server context, and the rest of runtime/ in the app context. Your module's own tsconfig.json checks them in the same contexts, as described in TypeScript. See Augment Types in the Module Author Guide.
The build transpiles each file on its own into dist/runtime/, and keeps its name and its imports. Build Output describes how each kind of file is emitted.
Nuxt doesn't apply auto-imports to files in node_modules, so runtime files can't rely on them. Import what you use from #imports, or from the package that provides it:
import { defineNuxtPlugin, useRuntimeConfig } from '#imports'
export default defineNuxtPlugin(() => {
const { greeting } = useRuntimeConfig().public.myModule
return {
provide: {
greet: (name: string) => `${greeting}, ${name}!`,
},
}
})The .nuxt that prepare generates for your module disables auto-imports too. A missing import is then a type error, in your editor and in the build.
To transpile another directory in the same way, add it to builder({ entries }).
The playground is a Nuxt app that installs your module, so you can try the module while you develop it. It installs the module by its package name:
export default defineNuxtConfig({
modules: ['my-module'],
myModule: {
greeting: 'Hi',
},
})The playground has to resolve that name to the module directory. With pnpm, list playground in pnpm-workspace.yaml, and install the module with link:..:
{
"name": "my-module-playground",
"private": true,
"type": "module",
"dependencies": {
"my-module": "link:.."
}
}link:.. works with Yarn 4 and bun too. npm needs file:.. instead. Installing the module in the playground lists the setup for each package manager, and the allowBuilds that pnpm needs in pnpm-workspace.yaml.
The playground loads the module through the exports of package.json. With pnpm or Yarn 4, exports point at your sources, and publishConfig.exports point at dist for publishing. With npm, Yarn 3 or earlier, or bun, exports point at dist, and tsdown --watch rebuilds dist as you change your sources. Develop and Publish describes both setups.
A playground can also list the module by relative path:
export default defineNuxtConfig({
modules: ['../src/module'],
})It then loads src/module.ts whichever setup package.json uses, and doesn't resolve the package name, so it works the same with every package manager. It only needs the dependency on my-module when the playground or a runtime file imports the module by name. Its type check doesn't read the module's types export, so it doesn't see the module's hooks and runtime config types: those of ModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig and ModulePublicRuntimeConfig.
This is a minimal package.json for a module that you publish with pnpm or Yarn 4:
{
"name": "my-module",
"version": "1.0.0",
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./.nuxt/module-builder/types.d.mts",
"import": "./src/module.ts"
}
},
"main": "./src/module.ts",
"files": [
"dist"
],
"publishConfig": {
"exports": {
".": {
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
}
},
"main": "./dist/module.mjs"
},
"scripts": {
"prepack": "tsdown",
"dev": "nuxt-module-build prepare && nuxt dev playground",
"dev:prepare": "nuxt-module-build prepare && nuxt prepare playground"
},
"dependencies": {
"@nuxt/kit": "^4.0.0"
},
"devDependencies": {
"@nuxt/module-builder": "^2.0.0-alpha.0",
"nuxt": "^4.0.0",
"tsdown": "^0.23.7",
"typescript": "^5.9.3 || ^6.0.0"
}
}exportsandmainpoint at your sources while you develop.exports["."].typespoints at the declarations thatpreparewrites. They re-exportsrc/module.ts, together with the augmentations above.publishConfigholds the fields that pnpm and Yarn 4 publish in place of the top-level ones. Itstypescondition points atdist/types.d.mts, the built equivalent. See Source exports.dependencies,peerDependencies, andoptionalDependenciesstay external todist/module.mjs. The build bundles any other package that the module entry imports, such as adevDependency.
npm, Yarn 3 and earlier, and bun publish some or all of these fields as written. If you publish with one of them, point exports and main at dist, leave out publishConfig, and build dist in dev:prepare:
{
"exports": {
".": {
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
}
},
"main": "./dist/module.mjs",
"scripts": {
"dev:prepare": "nuxt-module-build prepare && tsdown && nuxt prepare playground"
}
}Every build reads the paths that package.json publishes, with the fields of publishConfig in place of the top-level ones. The build fails when one of them points at a TypeScript source, or at a file that the pack leaves out. It warns about any path that the build didn't produce. See Checks on published paths.
To publish more entry points, such as my-module/utils or my-module/components, see Exports.
Your module's own tsconfig.json references the tsconfigs that prepare generates in the module's .nuxt, one for each part of the module. It also references tsconfig.test.json, for tsdown.config.ts and the tests, which are in none of the parts:
{
"references": [
{ "path": "./.nuxt/tsconfig.node.json" },
{ "path": "./.nuxt/tsconfig.app.json" },
{ "path": "./.nuxt/tsconfig.server.json" },
{ "path": "./.nuxt/tsconfig.shared.json" },
{ "path": "./tsconfig.test.json" }
],
"files": [],
"exclude": ["dist", "node_modules", "playground"]
}TypeScript describes this setup, and how to change the options of one part. Tests and config files shows tsconfig.test.json.
tsdown.config.ts is the build configuration. It adds the builder() plugin to tsdown:
import { builder } from '@nuxt/module-builder/tsdown'
import { defineConfig } from 'tsdown'
export default defineConfig({
plugins: [builder()],
})tsdown and nuxt-module-build build both read it. Configuration lists its options.