Sub topic 6
Setting up the CLI and adding components
This chapter assumes you have a React project with Tailwind CSS already working (Vite or Next.js), basic TypeScript, and npm. You will initialize shadcn/ui, add two components, and see what the CLI changes in your project.
What the CLI does
shadcn/ui is not installed as a package you import from. The CLI is a tool that writes files into your repository. After that, the component code belongs to you.
There are two commands to know:
initconfigures the project and writes acomponents.jsonfile.addcopies the source of the components you name into your codebase.
npx shadcn@latest init
npx shadcn@latest add button dialogRunning init
init inspects your project and asks a few questions. Typical prompts cover the base color and whether to use CSS variables for theming. It then does the following:
- Writes
components.json, which records your choices. - Creates the
cnhelper inlib/utils.ts. The next chapter covers it. - Adds theme variables to your global CSS file.
- Installs the packages the helper and styles need, such as
clsxandtailwind-merge.
The exact prompts and generated files change between CLI versions and between Vite and Next.js. Read the output of your own run instead of expecting an identical result.
Reading components.json
components.json is how the CLI knows where to put things. You will usually see these kinds of settings:
| Setting | Purpose |
|---|---|
| Style | The visual style of the copied components |
| Tailwind config and CSS path | Where theme variables are written |
| Base color | The starting color palette |
| CSS variables | Whether theming uses variables |
| Aliases | Import paths such as `@/components` and `@/lib/utils` |
The aliases matter most. Copied components import cn from the utils alias, for example @/lib/utils. If your tsconfig.json path alias does not match the one in components.json, imports fail.
Adding components
add takes one or more component names. For button and dialog, it does three things:
- Writes
button.tsxanddialog.tsxinto the directory set by youruialias, commonlycomponents/ui. - Installs the dependencies those files need. Dialog needs the Radix dialog package.
- Asks before overwriting a file that already exists.
Open the generated button.tsx. It is ordinary TSX in your repository. You can read it, edit it, and review it in a pull request like any other file.
Then use it:
import { Button } from "@/components/ui/button"
export function Example() {
return <Button variant="outline">Save</Button>
}What to commit
Commit components.json, the copied component files, and the changes to your CSS. They are source code. Because you own them, later upgrades are not automatic. A later chapter covers keeping them current.
Exercise
You run npx shadcn@latest add dialog and the build fails with a module-not-found error on @/lib/utils. Which two files do you compare to find the cause?
Show answer
Compare the paths alias in tsconfig.json with the aliases section of components.json. The copied component imports cn through the alias in components.json, so the path alias in your TypeScript and bundler setup must resolve the same path.