Shadcn Nuxt Module
shadcn-nuxt is a Nuxt.js module designed to seamlessly integrate shadcn/ui components (specifically the Vue implementation, shadcn-vue) into Nuxt 3 applications. It automates the setup and configuration process, offering features like automatic import of components into your Nuxt project, abstracting away manual configuration of Tailwind CSS and component paths. The current stable version is 2.6.2, with frequent bug fixes and feature enhancements, indicating an active development cycle. Key differentiators include its robust auto-import mechanism, support for multiple component directories with custom prefixes, and alignment with the upstream shadcn-ui API for consistency. It aims to simplify the developer experience by providing a pre-configured solution for using shadcn-vue's UI library within the Nuxt ecosystem.
Common errors
-
Cannot find module 'shadcn-nuxt' or its corresponding type declarations.
cause The package was not installed or installed incorrectly, or `nuxt.config.ts` is not correctly configured for TypeScript.fixEnsure `shadcn-nuxt` is installed as a dev dependency (`pnpm add -D shadcn-nuxt` or `npm install --save-dev shadcn-nuxt`) and that `nuxt.config.ts` is a TypeScript file. -
Component is not defined
cause The component has not been added to the project via `shadcn-vue add`, or the `componentDir` in `nuxt.config.ts` is incorrect, preventing auto-import.fixRun `npx shadcn-vue add <ComponentName>` to scaffold the component into your project. Verify that `shadcn.componentDir` in `nuxt.config.ts` correctly points to the directory where your shadcn-vue components reside (e.g., `./components/ui`). -
Failed to resolve component: <ComponentName>
cause The component has not been added to the project via `shadcn-vue add`, or the `componentDir` in `nuxt.config.ts` is incorrect, preventing auto-import.fixRun `npx shadcn-vue add <ComponentName>` to scaffold the component into your project. Verify that `shadcn.componentDir` in `nuxt.config.ts` correctly points to the directory where your shadcn-vue components reside (e.g., `./components/ui`). -
Tailwind CSS compilation error: 'Your custom CSS contains `tailwind` directives that cannot be used with your current `plugins` configuration.'
cause Tailwind CSS is not correctly configured in your Nuxt project, or the `shadcn-nuxt` module's Tailwind integration is conflicting with an existing setup.fixEnsure Tailwind CSS is correctly installed and configured in your Nuxt project, including `postcss.config.js`. Verify that `shadcn-nuxt` is listed in your `modules` array. If you have a custom Tailwind configuration, check for conflicts with how `shadcn-nuxt` expects it to be set up. Consult the `shadcn-nuxt` documentation for specific Tailwind integration steps.
Warnings
- breaking The `shadcn-nuxt` module updated its internal API to align with `shadcn-ui` in v2.6.0. While specific breaking changes are not explicitly listed, this often implies changes in configuration options or component usage patterns for closer parity with the upstream `shadcn-ui` philosophy.
- gotcha Configuring multiple component directories with `componentDir` as an array (e.g., `['@/components/ui', { path: '@/components/ai', prefix: 'Ai' }]`) is supported to prevent Nuxt's default auto-import scanning from causing console warnings. Failing to configure these paths correctly within the `shadcn` module options can lead to unexpected import behavior or warnings.
- gotcha While `shadcn-nuxt` sets up the module integration, it does not automatically install or scaffold individual Shadcn Vue components. Developers still need to run `npx shadcn-vue add <component-name>` (or the equivalent `npx nuxt shadcn-vue add <component-name>` if `shadcn-nuxt` provides a CLI wrapper) to fetch the component code into their project's `componentDir`.
- gotcha Older versions of `shadcn-nuxt` or `shadcn-vue` CLI might have had issues resolving `tsconfig.json` path aliases correctly, particularly for nested paths or when multiple alias prefixes were used. This could lead to component not found errors or incorrect scaffolding.
Install
-
npm install shadcn-nuxt -
yarn add shadcn-nuxt -
pnpm add shadcn-nuxt
Imports
- "shadcn-nuxt"
import shadcnNuxt from 'shadcn-nuxt'
export default defineNuxtConfig({ modules: [ 'shadcn-nuxt' ] }) - shadcn (config property)
import { shadcn } from 'shadcn-nuxt'export default defineNuxtConfig({ shadcn: { prefix: '', componentDir: './components/ui' } }) - Components (e.g., Button)
import { Button } from '@/components/ui/Button.vue'<template> <Button>Click me</Button> </template>
Quickstart
import { defineNuxtConfig } from 'nuxt'
export default defineNuxtConfig({
modules: [
'shadcn-nuxt'
],
shadcn: {
/**
* Prefix for all the imported component
*/
prefix: '',
/**
* Directory that the component lives in.
* @default "./components/ui"
*/
componentDir: './components/ui'
}
})
// In a Vue component (e.g., app.vue or any page/component file)
<script setup lang="ts">
// Button component is auto-imported by shadcn-nuxt
</script>
<template>
<div>
<h1>Welcome to Nuxt with Shadcn Vue!</h1>
<Button>Click me</Button>
</div>
</template>