ui: [bug]: Components refer to wrong "utils.ts" file after "utils.ts" file renamed and moved to different folder [Remix]
Describe the bug
when adding a new components for example npx shadcn@latest add button. usually the components importing the generated “utils” file, however, i rename the file to “cn.ts” and move it from ~/lib/utils to ~/lib/utils/cn. I also change the generated components.json file and refer the moved utils file to correct path.
here’s my components.json file:
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "default",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.ts",
"css": "app/tailwind.css",
"baseColor": "neutral",
"cssVariables": false,
"prefix": ""
},
"aliases": {
"components": "~/components",
"utils": "~/lib/utils/cn", // before was ~/lib/utils
"ui": "~/components/ui",
"lib": "~/lib",
"hooks": "~/hooks"
}
}
also my tsconfig.json file (i’m not changing anything):
{
"include": [
"**/*.ts",
"**/*.tsx",
"**/.server/**/*.ts",
"**/.server/**/*.tsx",
"**/.client/**/*.ts",
"**/.client/**/*.tsx"
],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"types": ["@remix-run/node", "vite/client"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"module": "ESNext",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"target": "ES2022",
"strict": true,
"allowJs": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": ["./app/*"]
},
// Vite takes care of building everything, not tsc.
"noEmit": true
}
}
I do the same thing when working with Nextjs project, and everything works fine, but in Remix, this happened.
Affected component/components
Probably all components, but i only try it on Button, Label, and Form
How to reproduce
- install shadcn ui in Remix project and follow all instruction.
- create new folder inside
libfolder namedutils - move
utils.tsto insideutilsfolder - rename
utils.tsintocn.ts - in
components.json, replace"utils": "~/lib/utils",to"utils": "~/lib/utils/cn", - add components like usual, for example
npx shadcn@latest add button - after component installed, open
button.ts - see that the components have an error
cannot find module ~/lib/utils
Codesandbox/StackBlitz link
No response
Logs
No response
System Info
Nothing relevant
Before submitting
- I’ve made research efforts and searched the documentation
- I’ve searched for existing issues
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 14
- Comments: 20
+1
Use the downgraded version (2.1.6) fixed my issue
Not sure if this relates, but noticed that the utils path also is wrong when changing the
@workspacename. If I spin up a new monorepo usingpnpm dlx shadcn@canary initand chooseNext.js (Monorepo)- then when I run an install of a component like:pnpm dlx shadcn@canary add dialog -c apps/web, it works perfectly fine, and importscncorrectly fromimport { cn } from "@workspace/ui/lib/utils".However, if I replace every instance of
@workspacewith something else like@repobefore running the add command, the import now omits the/uifrom the path.Mind you - the
components.jsonfiles haven’t changed aside from the@repoprefix:Even by changing the
aliases.utilsto something else like"utils": "Hello World!", it is ignored and remainsimport { cn } from "@repo/lib/utils". However - if the ui alias is different inapps/web/components.json(for example:"ui": "@foo/ui/components") theutilsalias is preserved (despite of course, now the package is being created in a sub directory in the web directory).I’m encountering same issue.
liborutilsconfig aliases seems to be ignored.I’ll take a look.
Hi everyone,
I’ve been testing this issue myself since I encountered it as well. With the current support for ShadCN in an NX monorepo, having to manually correct the imports for each component became unmanageable.
As @andrewbrey pointed out, older versions seem to work fine, but version 2.1.8 introduces the problem. I’ve reviewed recent versions to pinpoint where the functionality started breaking.
It appears that shadcn@2.1.6 works correctly and generates the appropriate imports. However, starting from 2.1.7, the behaviour changes unexpectedly: the
@namespace is ignored, and the imports default to@namespace/lib/utils, which is quite frustrating.As a temporary workaround, you can downgrade the CLI to version 2.1.6, which resolves the issue for now.
For example:
CLI:
npx shadcn@2.1.6 add buttonButton.tsx(Correct):CLI:
npx shadcn@2.1.7 add buttonButton.tsx(Incorrect):For reference, here are the relevant configurations:
components.json:
tsconfig.json:
I hope this helps others encountering the same issue. I’ll try to find some time to review the code and create a PR to address this.
@andrewbrey Hello sir, sorry for not replying as soon as possible, and seems like your advice is indeed works, i changed the path alias from
~/appinto@/appand it works very well, thankyou, maybe the downside is i only need change the path alias. But i hope in the future, shadcn-ui team would fix this.once again, thank you
@imMatheus I guess the
button.tsstill containsimport { cn } from "~/lib/utils"instead ofimport { cn } from "~/lib/utils/cn".Been banging my head against this issue too. Just adding a / to the components alias fixes the utils import. Hope this gets fixed.
This creates "import { cn } from “@components/lib/utils”
This creates "import { cn } from “@utils/cn”
same issue, I configured this as :
in any component it imported from
import { cn } from "~/lib/utils"instead it should imported fromimport { cn } from "@rd/shared"