Sub topic 6
Setting up the CLI and adding components
This chapter assumes you can work in a React and TypeScript project (Vite or Next.js) with Tailwind CSS already installed, and that you know what the shadcn/ui model is: you copy component source into your repository and own it. Here you will run the CLI, read the configuration it writes, and learn what happens when you add components. You will also learn the errors people most often hit.
What the CLI does
The shadcn CLI is not a runtime library. It is a tool that reads a configuration file, fetches component definitions from a registry, and writes source files into your project. Your app never imports from shadcn. After a component is added, it behaves like any other file you wrote.
Two commands cover most of the work:
npx shadcn@latest init
npx shadcn@latest add button dialoginit configures the project and creates components.json. add copies the chosen component source files into your codebase. The @latest tag makes npx fetch the current CLI version each time, so you don't depend on a stale global install.
Running init
Run init from the project root, where package.json lives. The CLI inspects the project to find your framework, your TypeScript setup, your Tailwind version, and the path alias configuration. It then asks a small number of questions. The exact prompts change between CLI versions, but they usually cover these choices:
- The base color, such as neutral, slate, or zinc. This sets the starting palette of the CSS variables.
- Whether to use CSS variables for colors. The default and recommended answer is yes. Chapter 9 covers theming in detail.
- In older versions, the component style and the location of your global CSS file and Tailwind config.
When it finishes, init has typically done four things:
- Written
components.json. - Created
lib/utils.tscontaining thecnhelper. - Added color variables and base layer rules to your global CSS file.
- Installed the packages the helper and most components need, including
class-variance-authority,clsx,tailwind-merge, and an icon library such aslucide-react.
Review the diff with git diff before you commit. The CLI edits existing files, and you should know which lines it touched.
Path aliases must exist first
The CLI writes imports like @/lib/utils. That only works if your project resolves the @/ alias. In a Vite project you must set it in both tsconfig.json and vite.config.ts. In Next.js the alias is usually set up for you. If the alias is missing, init may fail, or components may be written correctly but fail to compile with "Cannot find module '@/lib/utils'".
A typical tsconfig.json fragment looks like this:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}In Vite, the bundler needs its own matching setting, because TypeScript path mapping does not change how Vite resolves imports:
import path from "path"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
})Importing path in a Vite config requires the @types/node package for TypeScript to accept it.
Reading components.json
components.json is the contract between your project and the CLI. The CLI reads it on every add. A representative file looks like this:
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.ts",
"css": "src/index.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
}
}The fields you will actually touch are these:
| Field | Meaning | Common reason to edit |
|---|---|---|
| `style` | The visual flavor of the component source | Fixed at init. Changing it later does not rewrite files you already have. |
| `rsc` | Whether to add the `"use client"` directive to components | Set to `true` for Next.js App Router projects. |
| `tsx` | Whether to emit TypeScript or plain JavaScript | Set to `false` for a JavaScript project. |
| `tailwind.css` | The global CSS file that holds the theme variables | Correct it if your CSS file lives somewhere else. |
| `tailwind.baseColor` | The palette used to generate variables | Fixed at init. |
| `tailwind.cssVariables` | Theme with variables instead of inline utility colors | Fixed at init. |
| `tailwind.prefix` | A prefix added to all Tailwind classes | Set it when your app already has class name collisions. |
| `aliases.ui` | Where `add` writes component files | Change it to move components to another folder. |
In Tailwind v4 projects, the tailwind.config value is an empty string, because v4 configures the theme in CSS instead of a JavaScript file. Do not "fix" the empty value by adding a path.
The alias values are import specifiers, not file system paths. If you change aliases.ui, make sure the new alias resolves through tsconfig.json too.
Adding components
Pass one or more component names to add:
npx shadcn@latest add button dialogWith no names, the CLI shows an interactive list. Useful flags include --overwrite to replace existing files, --yes to skip confirmation prompts, and --all to add every component. Run npx shadcn@latest add --help to see the options your version supports.
For each component the CLI does the following:
- Looks up the component's definition in the registry.
- Resolves its registry dependencies. For example,
alert-dialogneedsbutton, so the CLI addsbuttontoo. - Installs the npm packages the component imports, such as
@radix-ui/react-dialog. - Writes the source file into the folder named by
aliases.ui, usuallycomponents/ui/dialog.tsx.
Then you import and use it like any local module:
import { Button } from "@/components/ui/button"
export function Toolbar() {
return <Button variant="outline">Save draft</Button>
}The registry is a set of JSON files served over HTTP. Each entry lists the file contents, the npm dependencies, and the other registry items it needs. That is why the CLI needs network access when you run add, and why nothing about this setup requires you to be online while the app runs.
What happens when a file already exists
If components/ui/button.tsx exists, the CLI asks before replacing it, because your edits would be lost. Answer no unless you want the upstream version. With --overwrite you skip the prompt, so run it only on a clean Git working tree. That way you can review the replacement and recover your changes with git checkout or git stash.
Common errors
- Cannot find module
@/lib/utils. The path alias is missing intsconfig.json, the bundler config, or both. Fix the alias, not the import. - Components render with no colors. The global CSS file that
initedited is not imported by your app entry point, ortailwind.cssincomponents.jsonpoints to a different file than the one you load. - A component works in development but the build fails on an unknown utility class. Your Tailwind version and the component source disagree. Check that you initialized with the Tailwind version actually installed.
"use client"errors in Next.js. Components that use state or event handlers must be client components. Setrsctotruebefore adding them so the directive is included.- Peer dependency errors from npm. The CLI may prompt you to choose a flag such as
--forceor--legacy-peer-depswhen a package declares a React version range that does not include yours. Prefer the narrower choice and check that the app still runs.
Checking your setup
A quick verification is to add one component and render it. If the button appears with padding, a background color, and a visible focus ring when you press Tab, the files, the aliases, the CSS variables, and the Tailwind pipeline all work together. If any of those pieces is missing, one of the errors above will show up here, and you can fix it before adding more components.
Exercise
You run npx shadcn@latest add button in a Vite project. The file components/ui/button.tsx appears, but the build fails with "Cannot find module '@/lib/utils'". Which two configuration files should you check, and why do you need both?
Show answer
Check tsconfig.json and vite.config.ts. The paths entry in tsconfig.json tells TypeScript how to resolve @/, but Vite resolves imports on its own, so it needs a matching resolve.alias entry. The CLI also expects aliases.utils in components.json to match the same alias. If lib/utils.ts was never created, rerun init or create the cn helper by hand.
Exercise
A teammate edited components/ui/dialog.tsx to add a custom close button. Later they run npx shadcn@latest add dialog --overwrite. What happens, and how do you make the command safe to run?
Show answer
The CLI replaces the whole file with the registry version, so the custom close button is lost, because there is no merge. Commit or stash the work before running it, then review the result with git diff and re-apply the custom parts by hand if you still want them.