Skip to content

Instantly share code, notes, and snippets.

@infomiho
Last active March 27, 2025 23:38
Using ShadCN with Wasp 0.12+

Using ShadCN with Wasp 0.12 and beyond

Note

We'll be loosly following the Vite instructions for ShadCN since Wasp is using Vite + React: https://ui.shadcn.com/docs/installation/vite We'll skip some of the steps since they don't apply or they are done differently with Wasp.

You won't be able to use the @ alias setup since it's not currently supported by Wasp. Because of this you'll need to adjust some imports when we generate components, but it should be fairly straightforward to do.

1. Enable TailwindCSS if you haven't already

Based on: https://wasp-lang.dev/docs/project/css-frameworks#enabling-tailwind-step-by-step

Add ./tailwind.config.cjs:

const { resolveProjectPath } = require('wasp/dev')

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [resolveProjectPath('./src/**/*.{js,jsx,ts,tsx}')],
  theme: {
    extend: {},
  },
  plugins: [],
}

Add ./postcss.config.cjs:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

Run wasp start to make sure that npm dependencies are installed.

2. Temporarily set up the @ alias (which we'll remove later)

We need to temporarily setup the @ alias to pass the ShadCN preflight checks.

Adjust the tsconfig.json:

// =============================== IMPORTANT =================================
//
// This file is only used for Wasp IDE support. You can change it to configure
// your IDE checks, but none of these options will affect the TypeScript
// compiler. Proper TS compiler configuration in Wasp is coming soon :)
{
  "compilerOptions": {
    // ...
+   "baseUrl": ".",
+   "paths": {
+     "@/*": ["./src/*"]
+   }
  }
}

3. Setup ShadCN

Go into your project dir and run:

npx shadcn@2.3.0 init

Select the options like this:

✔ Preflight checks.
✔ Verifying framework. Found Vite.
✔ Validating Tailwind CSS.
✔ Validating import alias.
✔ Which style would you like to use? › New York
✔ Which color would you like to use as the base color? › Neutral
✔ Would you like to use CSS variables for theming? … yes
✔ Writing components.json.
✔ Checking registry.
✔ Updating tailwind.config.cjs
✔ Updating src/Main.css
✔ Installing dependencies.
✔ Created 1 file:
  - src/lib/utils.ts

4. Remove the @ alias

Adjust the tsconfig.json:

// =============================== IMPORTANT =================================
//
// This file is only used for Wasp IDE support. You can change it to configure
// your IDE checks, but none of these options will affect the TypeScript
// compiler. Proper TS compiler configuration in Wasp is coming soon :)
{
  "compilerOptions": {
    // ...
-   "baseUrl": ".",
-   "paths": {
-     "@/*": ["./src/*"]
-   }
  }
}

5. Adjust the components.json

Adjust the aliases in components.json to be:

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "new-york",
  "rsc": false,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.cjs",
    "css": "src/Main.css",
    "baseColor": "neutral",
    "cssVariables": true,
    "prefix": ""
  },
  "aliases": {
-   "components": "@/components",
+   "components": "src/components",
-   "utils": "@/lib/utils",
+   "utils": "src/lib/utils",
-   "ui": "@/components/ui",
+   "ui": "src/components/ui",
-   "lib": "@/lib",
+   "lib": "src/lib",
-   "hooks": "@/hooks"
+   "hooks": "src/hooks"
  }
}

6. Let's add a new component

We'll add a button component with:

npx shadcn@2.3.0 add button

7. Adjust the utils import in button.tsx (for each component you add)

You'll notice that you now have a brand new button.tsx file in src/components/ui. We need to fix some import issues:

import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"

-import { cn } from "s/lib/utils"
+import { cn } from "../../lib/utils"

8. Use the Button component

Now you are ready to use the Button component. That's it!

import './Main.css'

import { Button } from './components/ui/button'

export const MainPage = () => {
  return (
    <div className="container">
      <Button>This works</Button>
    </div>
  )
}
@SarkarKurdish
Copy link

Thanks

@grsh50
Copy link

grsh50 commented Jun 18, 2024

Now i have two location of button component. One in src/components/ui and another /.wasp/sdk/wasp/ext-src/components/ui/button

wasp should update this path /.wasp/sdk/wasp/ext-src/components/ui/button based on code in src/components/ui ?

@SarkarKurdish
Copy link

SarkarKurdish commented Jun 25, 2024

@grsh50

Now i have two location of button component. One in src/components/ui and another /.wasp/sdk/wasp/ext-src/components/ui/button

wasp should update this path /.wasp/sdk/wasp/ext-src/components/ui/button based on code in src/components/ui ?

Ignore /.wasp/sdk/wasp/ext-src/components/ui/button this is generated during runtime just focus on the one in the src folder

@grsh50
Copy link

grsh50 commented Jun 26, 2024

That's what I thought, but for some reason these files were not overwritten after recompilation
/.wasp/sdk/wasp/ext-src/components/ui/button

@infomiho
Copy link
Author

Come to the Wasp Discord and let's figure it out together https://discord.gg/rzdnErX

@arresejo
Copy link

arresejo commented Sep 9, 2024

It's seems the CLI library name has changed, so the new command would be npx shadcn@latest init

@infomiho
Copy link
Author

Updated the gist to reflect the new CLI name :)

@gevalinho
Copy link

I am using WSL I tried every resource I saw online but it did not work for me finally what worked was running npm i shadcn-ui (https://www.npmjs.com/package/shadcn-ui). However, after installation,
I encountered a high-severity issue related to lodash.template. I fixed it using npm audit fix --force.
Then I ran npx shadcn-ui init

This solution is for those using WSL with Windows.
shadcn

@jifffffy
Copy link

jifffffy commented Jan 7, 2025

How to use chakra ui 3.0 which use the same tech likes shadcn.
shadcn cli has a monorepo flag , is it ok in wasp?

@vince-alcivar
Copy link

vince-alcivar commented Feb 21, 2025

I couldn't get shadcn to install using the npx shadcn@latest init command on Wasp 0.16. I got the following error:

✔ Verifying framework. Found Vite.
✖ Validating Tailwind CSS.
✔ Validating import alias.

No Tailwind CSS configuration found at [...redacted]/app.
It is likely you do not have Tailwind CSS installed or have an invalid configuration.
Install Tailwind CSS then try again.
Visit https://tailwindcss.com/docs/guides/vite to get started.

I tried following the instructions here by adding an index.css file with the expected tailwind headers but still got the same error.

The only thing that worked for me was specifying the version of shadcn, in my case npx shadcn@2.3.0 init

@vincanger
Copy link

change install step to npx shadcn@2.3.0 init

@alexandre-ehrd
Copy link

Thank you 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment