ui: Components added to wrong folder from CLI

I have the following components.json file:

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "rsc": false,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "styles/globals.css",
    "baseColor": "slate",
    "cssVariables": true
  },
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils"
  }
}

When I run shadcn-ui@latest add, it adds the file to a new directory: @/components/ui/ rather than my existing components/ui directory.

Expected location: C:\dev\project\components\ui\accordion.tsx Actual location: C:\dev\project\@\components\ui\accordion.tsx

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 31 (3 by maintainers)

Commits related to this issue

Most upvoted comments

The default behavior still seems incorrect to me.

The CLI add command out of the box adds components to a root @ folder which is not where it is expected to go

Hi @jarrodwatts, @mohamedaleya is right, you need to have this path in your tsconfig.json:

"paths": {
    "@/*": ["./*"]
}

Not sure if anyone went through the same problem lately, but I was working on a vite project where I needed to put the compiiler options in the ts.config.app.json to paths to work, instead of the tsconfig.json, but removing the compiler options and paths from the tsconfig.json broke shadcn, making it add any component requested to the root of the folder in @components/ui instead of src/components/ui.

So the solution was simple, albeit weird, I just left the compilerOptions config with the paths in both tsconfig.app.json and tsconfig.json files.

Same here.

I added “baseUrl” and “paths” into tsconfig.app.json, it fixed the import using @ alias in the .tsx issue. But seems like the shadcn-ui cli doesn’t care about the “compilerOptions” in tsconfig.app.json, it only cares about “compilerOptions” in tsconfig.app.

Being said, if you add “compilerOptions” section with “baseUrl” and “paths” into tsconfig.json as well, the shadcn cli can pickup the settings and add components to the correct folder in “src”.

It makes me believe this issue might be a bug in the cli.

I have the same issue. tsconfig.app.json

{
  "compilerOptions": {
    "composite": true,
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },

components.json

  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils"
  }
}

When I run npx shadcn-ui@latest add badge, it just add the badge outside the src folder. It creates a new “@” folder, and put the badge inside of “@/components/ui”

For me, this was a Vite-specific problem, having to do with the way Vite organizes and separates out web (app) and devtool (node) tsconfig files, as @Murad71 pointed out.

While I’m sure @Murad71’s solution works (I haven’t tried it), I fear it might interfere with Vite functioning correctly…after all, Vite does it that way for a reason (not saying I know what that reason is, exactly, but everything in Vite seems to be very intentional).

Here’s what works for me without changing the way Vite splits the tsconfig files:

In both tsconfig.json and tsconfig.app.json, add the baseUrl and paths properties (in compilerOptions), however you would like them configured.

It feels like a slightly hacky solution…would prefer to put it in one place. But this makes everything work (resolution in VSCode, building, and running in dev mode, and using shadcn/ui to add components puts them where they should be).

Vite

On Thu, Jul 11, 2024, 10:23 PM shadcn @.***> wrote:

shadcn does not recognise the tsconfig.app.json

Interesting. Which framework uses tsconfig.app.json as default? I’ll try and reproduce this.

— Reply to this email directly, view it on GitHub https://github.com/shadcn-ui/ui/issues/1092#issuecomment-2223359592, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJRYAWKCEEHIEDSHZRI4DZTZL2WOTAVCNFSM6AAAAAA27SGBCKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRTGM2TSNJZGI . You are receiving this because you commented.Message ID: @.***>

I faced a similar problem. Now coming to the fix.

  1. Copy everything from tsconfic.app.json to tsconfig.json (erase whatever tsconfig.json had previously)
  2. Delete tsconfig.app.json and tsconfig.node.json.
  3. Only after that should you run the command : ‘npm i -D @types/node’ as usual.

You have to follow the order. Or else it won’t work. Unfortunately, Shadcn does not recognise the tsconfig.app.json file which is the root cause of all the issues. Hopefully, they fix it.

tsconfig.json:

{
  "paths": {"@ui/": ["web/ui/*"]}
}

components.json:

{
  "aliases": {"components": "@ui/*"}
}

But the add command adds to web/ui/ui/ instead of web/ui - what am I doing wrong?

I have the same problem here, how can I add an open configuration for the aliases installation and others for imports inside the component?

When I config the components.json aliases with relative paths install works well (installed in the correct folder), but imports inside the components were wrong:

  "aliases": {
    "components": "src/renderer/src/components",
    "utils": "src/renderer/src/lib/utils",
    "ui": "src/renderer/src/components/ui",
    "lib": "src/renderer/src/lib",
    "hooks": "src/renderer/src/hooks"
  },
import * as React from "react"

import { cn } from "src/lib/utils" // ❌ broken import

When I config the components.json aliases with absolute import paths, the cli ignores and puts the components/ui folder in the root (instead of src/renderer/src), but imports inside the components work well:

  "aliases": {
    "components": "@renderer/components",
    "utils": "@renderer/lib/utils",
    "ui": "@renderer/components/ui",
    "lib": "@renderer/lib",
    "hooks": "@renderer/hooks"
  },
import * as React from "react"

import { cn } from "@renderer/lib/utils" // ✅ correct import

Environment: Vite + Electron https://electron-vite.org/

This issue still persist. ( Vite & Remix.js)

The issue is still relevant for me.

  "aliases": {
    "components": "@repo/ui/components",
    "ui": "@repo/ui/components",
    "utils": "@repo/ui/utils",
    "hooks": "@repo/ui/hooks"
  }
{
"extends": "@repo/typescript-config/react-library.json",
"compilerOptions": {
  "baseUrl": ".",
  "paths": {
    "@repo/ui/*": ["./src/*"]
  },
  "types": ["vitest/globals", "@testing-library/jest-dom"]
},
"include": [    
  "**/*.ts",
  "**/*.tsx",
],
"exclude": ["node_modules"]
}

and components are generated inside /ui/components/ui instead of /ui/components

Guys, are you sure you put paths inside compilerOptions in tsconfig.json?

If paths sits outside, it does nothing.

https://github.com/shadcn-ui/ui/issues/2068#issuecomment-1913611737

If someone finds this useful, here is what works for me: add -p to add the location path for the component. For example, I use pnpm and the location I want within my project is ./inertia/components/ui

pnpm dlx shadcn-ui@latest add carousel -p ./inertia/components/ui

Here is my components.json

  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "new-york",
  "rsc": false,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "./inertia/css/app.css",
    "baseColor": "slate",
    "cssVariables": false,
    "prefix": ""
  },
  "aliases": {
    "components": "#shadcn",
    "ui": "#shadcn",
    "utils": "#shadcn/utils"
  }
}

and my tsconfig.json

{
  "extends": "@adonisjs/tsconfig/tsconfig.app.json",
  "compilerOptions": {
    "rootDir": "./",
    "outDir": "./build",
    "paths": {
      "#controllers/*": ["./app/controllers/*.js"],
      "#exceptions/*": ["./app/exceptions/*.js"],
      "#models/*": ["./app/models/*.js"],
      "#mails/*": ["./app/mails/*.js"],
      "#services/*": ["./app/services/*.js"],
      "#listeners/*": ["./app/listeners/*.js"],
      "#events/*": ["./app/events/*.js"],
      "#middleware/*": ["./app/middleware/*.js"],
      "#validators/*": ["./app/validators/*.js"],
      "#providers/*": ["./providers/*.js"],
      "#policies/*": ["./app/policies/*.js"],
      "#abilities/*": ["./app/abilities/*.js"],
      "#database/*": ["./database/*.js"],
      "#tests/*": ["./tests/*.js"],
      "#start/*": ["./start/*.js"],
      "#config/*": ["./config/*.js"],
      "#shadcn/*": ["./inertia/components/ui/*"],
    }
  },
  "exclude": ["./inertia/**/*"]
}

I personally like the #shadcn (or @shadcn) alias for my projects, as well as keeping the dedicated utils.tswithin the components/uifolder.

I link a project with AdonisJS + Inertia if you want the real implementation. It works like a charm. https://github.com/akaRicane/ricane.art.adonis Cheers

You should be able to edit that prefix in your components.json file which is spawned after initializing shadcn/ui (using next.js 13.4.2 here). For example mine is like this:

components.json

"aliases": {
    "components": "~/components",
    "utils": "~/lib/utils"
  }

tsconfig.json

    "paths": {
      "~/*": ["./src/*"]
    }

I made the same mistake you did and this is how I was able to change it to the correct path. Hope this helps!

Hi @jarrodwatts what’s in your tsconfig.json?