Nuxt auto-imports helper functions, composables and Vue APIs to use across your application without explicitly importing them. Based on the directory structure, every Nuxt application can also use auto-imports for its own composables and plugins.
With Nuxt Kit you can also add your own auto-imports. addImports and addImportsDir allow you to add imports to the Nuxt application. addImportsSources allows you to add listed imports from 3rd party packages to the Nuxt application.
These utilities are powered by unimport, which provides the underlying auto-import mechanism used in Nuxt.
addImportsAdd imports to the Nuxt application. It makes your imports available in the Nuxt app context without the need to import them manually.
addServerImports function.twoslash
export default defineNuxtModule({
setup (options, nuxt) {
const names = [
'useStoryblok',
'useStoryblokApi',
'useStoryblokBridge',
'renderRichText',
'RichTextSchema',
]
names.forEach(name =>
addImports({ name, as: name, from: '@storyblok/vue' }),
)
},
})function addImports (imports: Import | Import[]): voidimports: An object or an array of objects with the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
name | string | true | Import name to be detected. |
from | string | true | Module specifier to import from. |
priority | number | false | Priority of the import; if multiple imports have the same name, the one with the highest priority will be used. |
disabled | boolean | false | If this import is disabled. |
meta | Record<string, any> | false | Metadata of the import. |
type | boolean | false | If this import is a pure type import. |
typeFrom | string | false | Use this as the from value when generating type declarations. |
as | string | false | Import as this name. |
addImportsDirAdd imports from a directory to the Nuxt application. It will automatically import all files from the directory and make them available in the Nuxt application without the need to import them manually.
twoslash
export default defineNuxtModule({
meta: {
name: '@vueuse/motion',
configKey: 'motion',
},
setup (options, nuxt) {
const resolver = createResolver(import.meta.url)
addImportsDir(resolver.resolve('./runtime/composables'))
},
})function addImportsDir (dirs: string | string[], options?: { prepend?: boolean }): void| Property | Type | Required | Description |
|---|---|---|---|
options | { prepend?: boolean }{lang="ts"} | false | Options to pass to the import. If prepend is set to true, the imports will be prepended to the list of imports. |
addImportsSourcesAdd listed imports to the Nuxt application.
twoslash
export default defineNuxtModule({
setup () {
addImportsSources({
from: 'h3',
imports: [
'defineEventHandler',
'getQuery',
'getRouterParams',
'readBody',
'sendRedirect',
],
})
},
})function addImportsSources (importSources: ImportSource | ImportSource[]): voidimportSources: An object or an array of objects with the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
from | string | true | Module specifier to import from. |