Files
gil-wiki/reference/4.api/5.kit/5.components.md
gil 5f664546cf feat: 위키 저장소 초기 커밋
- CLAUDE.md 운영 규칙
- wiki/ 정리된 지식 페이지 (Nuxt + Claude Code)
- raw/ 원본 자료
- reference/ Nuxt 4.x 공식 문서

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 00:31:51 +09:00

14 KiB

title, description, links
title description links
Components Nuxt Kit provides a set of utilities to help you work with components. You can register components globally or locally, and also add directories to be scanned for components.
label icon to size
Source i-simple-icons-github https://github.com/nuxt/nuxt/blob/main/packages/kit/src/components.ts xs

Components are the building blocks of your Nuxt application. They are reusable Vue instances that can be used to create a user interface. In Nuxt, components from the components directory are automatically imported by default. However, if you need to import components from an alternative directory or wish to selectively import them as needed, @nuxt/kit provides the addComponentsDir and addComponent methods. These utils allow you to customize the component configuration to better suit your needs.

::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/injecting-components-and-component-directories?friend=nuxt" target="_blank"} Watch Vue School video about injecting components. ::

addComponentsDir

Register a directory to be scanned for components and imported only when used. Keep in mind, that this does not register components globally, until you specify global: true option.

Usage

export default defineNuxtModule({
  meta: {
    name: '@nuxt/ui',
    configKey: 'ui',
  },
  setup () {
    addComponentsDir({
      path: resolve('./runtime/components'),
      prefix: 'U',
      pathPrefix: false,
    })
  },
})

Type

function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}): void

Parameters

dir An object with the following properties:

Property Type Required Description
path string true Path (absolute or relative) to the directory containing your components. You can use Nuxt aliases (~ or @) to refer to directories inside project or directly use an npm package path similar to require.
pattern string | string[]{lang="ts"} false Accept Pattern that will be run against specified path.
ignore string[] false Ignore patterns that will be run against specified path.
prefix string false Prefix all matched components with this string.
pathPrefix boolean false Prefix component name by its path.
prefetch boolean false These properties (prefetch/preload) are used in production to configure how components with Lazy prefix are handled by webpack via its magic comments. Learn more on webpack documentation
preload boolean false These properties (prefetch/preload) are used in production to configure how components with Lazy prefix are handled by webpack via its magic comments. Learn more on webpack documentation
isAsync boolean false This flag indicates, component should be loaded async (with a separate chunk) regardless of using Lazy prefix or not.
extendComponent (component: Component) => Promise<Component | void> | (Component | void){lang="ts"} false A function that will be called for each component found in the directory. It accepts a component object and should return a component object or a promise that resolves to a component object.
global boolean false If enabled, registers components to be globally available.
island boolean false If enabled, registers components as islands. You can read more about islands in <NuxtIsland/> component description.
watch boolean false Watch specified path for changes, including file additions and file deletions.
extensions string[] false Extensions supported by Nuxt builder.
transpile 'auto' | boolean{lang="ts"} false Transpile specified path using build.transpile. If set to 'auto', it will set transpile: true if node_modules/ is in path.

opts

Property Type Required Description
prepend boolean false If set to true, the directory will be prepended to the array with unshift() instead of push().

addComponent

Register a component to be automatically imported.

Usage

import { addComponent, createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: '@nuxt/image',
    configKey: 'image',
  },
  setup () {
    const resolver = createResolver(import.meta.url)

    addComponent({
      name: 'NuxtImg',
      filePath: resolver.resolve('./runtime/components/NuxtImg.vue'),
    })

    addComponent({
      name: 'NuxtPicture',
      filePath: resolver.resolve('./runtime/components/NuxtPicture.vue'),
    })
  },
})

Type

function addComponent (options: AddComponentOptions): void

Parameters

options: An object with the following properties:

Property Type Required Description
name string true Component name.
filePath string true Path to the component.
declarationPath string false Path to component's declaration file. It is used to generate components' type templates; if not provided, filePath is used instead.
pascalName string false Pascal case component name. If not provided, it will be generated from the component name.
kebabName string false Kebab case component name. If not provided, it will be generated from the component name.
export string false Specify named or default export. If not provided, it will be set to 'default'.
shortPath string false Short path to the component. If not provided, it will be generated from the component path.
chunkName string false Chunk name for the component. If not provided, it will be generated from the component name.
prefetch boolean false These properties (prefetch/preload) are used in production to configure how components with Lazy prefix are handled by webpack via its magic comments. Learn more on webpack documentation
preload boolean false These properties (prefetch/preload) are used in production to configure how components with Lazy prefix are handled by webpack via its magic comments. Learn more on webpack documentation
global boolean false If enabled, registers component to be globally available.
island boolean false If enabled, registers component as island. You can read more about islands in <NuxtIsland/> component description.
mode 'client' | 'server' | 'all'{lang="ts"} false This options indicates if component should render on client, server or both. By default, it will render on both client and server.
priority number false Priority of the component, if multiple components have the same name, the one with the highest priority will be used.

Examples

If you want to auto-import a component from an npm package, and the component is a named export (rather than the default), you can use the export option to specify it.

import { addComponent, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  setup () {
    // import { MyComponent as MyAutoImportedComponent } from 'my-npm-package'
    addComponent({
      name: 'MyAutoImportedComponent',
      export: 'MyComponent',
      filePath: 'my-npm-package',
    })
  },
})