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)
The default behavior still seems incorrect to me.
The CLI
addcommand out of the box adds components to a root@folder which is not where it is expected to goHi @jarrodwatts, @mohamedaleya is right, you need to have this path in your
tsconfig.json: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/uiinstead ofsrc/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” intsconfig.app.json, it only cares about “compilerOptions” intsconfig.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.
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.jsonandtsconfig.app.json, add thebaseUrlandpathsproperties (incompilerOptions), 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:
I faced a similar problem. Now coming to the fix.
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:
components.json:
But the
addcommand adds toweb/ui/ui/instead ofweb/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.jsonaliases with relative paths install works well (installed in the correct folder), but imports inside the components were wrong:When I config the
components.jsonaliases with absolute import paths, thecliignores and puts thecomponents/uifolder in the root (instead ofsrc/renderer/src), but imports inside the components work well:Environment: Vite + Electron https://electron-vite.org/
This issue still persist. ( Vite & Remix.js)
The issue is still relevant for me.
and components are generated inside
/ui/components/uiinstead of/ui/componentsGuys, 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/uiHere is my
components.jsonand my
tsconfig.jsonI personally like the
#shadcn (or @shadcn)alias for my projects, as well as keeping the dedicatedutils.tswithin thecomponents/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.jsontsconfig.jsonI 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?