| title | Exports |
|---|---|
| description | Publish more entry points than the module itself, such as utilities, composables, and components. |
The root export of a module is its definition, dist/module.mjs, with dist/types.d.mts for its types. The root also re-exports the named values and types that src/module.ts exports. Anything else that you want users to import by name needs its own subpath in the exports of package.json.
The examples on this page show the published exports, which point at dist. With source exports, put them in publishConfig.exports, and point each subpath of exports at its source. See Source exports.
A bundled entry, such as ./src/utils in builder({ entries }), builds into dist/<name>.mjs and dist/<name>.d.mts. Export it with both:
{
"exports": {
".": {
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
},
"./utils": {
"types": "./dist/utils.d.mts",
"import": "./dist/utils.mjs"
}
}
}Users then import it as my-module/utils. A bundled entry suits code that runs alongside the module, such as helpers for nuxt.config or for other modules:
import { defineMyModuleOptions } from 'my-module/utils'
export default defineNuxtConfig({
modules: ['my-module'],
myModule: defineMyModuleOptions({ greeting: 'Hi' }),
})TypeScript reads the types condition of each export under the bundler and node16 module resolution, which Nuxt apps use. node10 module resolution ignores exports, and these docs don't cover it. If your module keeps a top-level types field or typesVersions for it, see Update the paths in package.json in the upgrade guide.
You can export files in src/runtime/ too, from their output in dist/runtime/. An app can then import them explicitly, as well as through whatever the module registers for auto-import.
To export a set of components, re-export them from an index.ts in the same directory:
export { default as Button } from './Button.vue'
export { default as Card } from './Card.vue'Then point an export at its output:
{
"exports": {
".": {
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
},
"./components": {
"types": "./dist/runtime/app/components/index.d.ts",
"import": "./dist/runtime/app/components/index.js"
}
}
}An app that installs the module can now import each component with its full type:
<script setup lang="ts">
import { Button } from 'my-module/components'
</script>
<template>
<Button label="Save" />
</template>To register the same components for auto-import, pass the index to addComponentExports from @nuxt/kit. Each named export becomes a component, prefixed with prefix:
import { addComponentExports, createResolver, defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
meta: { name: 'my-module', configKey: 'myModule' },
setup() {
const resolver = createResolver(import.meta.url)
addComponentExports({
filePath: resolver.resolve('./runtime/app/components/index'),
prefix: 'My',
})
},
})<MyButton> and <MyCard> are then available without an import.
Composables work the same way. Export them from a file in the runtime directory, and point a subpath at its output:
export { useGreeting } from './useGreeting'{
"exports": {
"./composables": {
"types": "./dist/runtime/app/composables/index.d.ts",
"import": "./dist/runtime/app/composables/index.js"
}
}
}To register them for auto-import, pass each one to addImports, or pass their directory to addImportsDir:
import { addImports, createResolver, defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
meta: { name: 'my-module', configKey: 'myModule' },
setup() {
const resolver = createResolver(import.meta.url)
addImports({
name: 'useGreeting',
from: resolver.resolve('./runtime/app/composables/useGreeting'),
})
},
})::note
With exports that point at dist, these subpaths resolve once the module has been built. Source exports resolve them to your sources while you develop.
::
With source exports, exports point at your sources, and publishConfig.exports holds the paths in dist. The playground, and anything else that installs the module from the workspace, then loads every export from src/ without a build:
{
"exports": {
".": {
"types": "./.nuxt/module-builder/types.d.mts",
"import": "./src/module.ts"
},
"./utils": "./src/utils.ts",
"./components": "./src/runtime/app/components/index.ts"
},
"publishConfig": {
"exports": {
".": {
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
},
"./utils": {
"types": "./dist/utils.d.mts",
"import": "./dist/utils.mjs"
},
"./components": {
"types": "./dist/runtime/app/components/index.d.ts",
"import": "./dist/runtime/app/components/index.js"
}
}
}
}- Each subpath of
exportspoints at the source file that the build turns into the matching path ofpublishConfig.exports. publishConfig.exportsreplacesexportsas a whole, so it repeats every subpath.- The root export's
typescondition points at the declarations thatnuxt-module-build preparewrites. The type augmentations ofsrc/module.tsthen apply in the playground. - A subpath has no augmentations, so it points at its source alone.
- A subpath that code outside Nuxt imports, such as a plugin for a plain Vite app, can keep pointing at
distinexports. That code then loads the built file. Build the module before you use it.
The build checks the paths in publishConfig.exports, not the source paths in exports. See Checks on published paths.
A subpath points at its source when only Nuxt loads it: an app, through the module, its nuxt.config, or a runtime file. It points at dist when Node or a tool that can't read TypeScript loads it, such as a plain Vite app's vite.config.ts, an ESLint config, or a CLI. Build the module before that code runs.
Each path of exports mirrors its path in publishConfig.exports:
- A wildcard subpath with an extension in its
disttarget gets the source's extension:"./runtime/*": "./dist/runtime/*.js"becomes"./src/runtime/*.ts", for itstypescondition too. TypeScript substitutes the extension after it resolves the target, so a*.jstarget also finds the.tsfiles, but Node loads the target as written and finds no.jsfile there. - A wildcard subpath without an extension in its
disttarget, such as"./dist/runtime/components/*", takes the import's own extension. It points at the same path undersrcwhen every file it reaches keeps its name indist, such as.vueand.cssfiles, for itstypescondition too. When it also reaches scripts, whichdistnames.js, such as"./*": "./dist/*"or a"./runtime/*"for components and composables, it stays atdist. - A subpath written as a string stays a string:
"./registry": "./dist/registry.mjs"becomes"./registry": "./src/registry.ts". - A subpath with only a
typescondition that points at a file outsidedist, such as./client.d.ts, stays as it is, in both fields. - A subpath for the module entry, such as
"./module", points where the root export does. - An entry of
importspoints at the source of its target../dist/types.d.mtshas none: point it at./.nuxt/module-builder/types.d.mts, as the root export'stypescondition does. mainandmodulepoint at./src/module.ts, with thedistpaths inpublishConfig.- Other fields, such as
style, stay atdist. pnpm and Yarn 4 don't applypublishConfigto them. Astylecondition inexportsfollowsexports, and points at the source stylesheet.
prepare and the build check each bundled entry that exports or imports points at source, and the files it imports, because Node loads them. A subpath that points at a runtime file is left to the build, which transpiles it.
When Node loads a subpath from source, as a vite.config.ts or a script does, the file it reaches and the files that file imports follow the module code rules, such as JSON imports with an import attribute. To check a runtime file, import its subpath with Node from the module root:
node --input-type=module -e "await import('my-module/<subpath>')"Runtime code and module code can import the module's own package name, such as my-module/utils. The build never bundles these imports, so they resolve to the published files in the app that installs the module.
While the build generates declarations, these imports resolve to your sources instead, so they type-check before dist exists. With source exports, the build reads the subpaths and their dist paths from publishConfig.exports.