shadcn-ui / ui

Beautifully designed components that you can copy and paste into your apps. Accessible. Customizable. Open Source.
https://ui.shadcn.com
MIT License
75.75k stars 4.76k forks source link

Cannot find module '../../@/components/ui/button' and Cannot find module '@/lib/utils' or its corresponding type declarations. #1101

Closed msafdev closed 1 year ago

msafdev commented 1 year ago

So i just started ts + vite + tailwind. tried installing shadcn and the button component to test it out, but it gave this error message. tried looking for the same issue here but didnt find any solution. i have run npx shadcn-ui init and it still didnt work, i dont use react server component.

xn1cklas commented 1 year ago

Can you please share your import, your components.json and the folder structure of your project?

shivxmr commented 1 year ago

So i just started ts + vite + tailwind. tried installing shadcn and the button component to test it out, but it gave this error message. tried looking for the same issue here but didnt find any solution. i have run npx shadcn-ui init and it still didnt work, i dont use react server component.

You can try this npx shadcn-ui@latest init

msafdev commented 1 year ago

I solved this problem by just moving the downloaded components into a ui folder in my src/components/ui and also the lib/utils. i think i messed up when I first init the library.

stephanebruckert commented 1 year ago

I solved this problem by just moving the downloaded components into a ui folder in my src/components/ui and also the lib/utils. i think i messed up when I first init the library.

works, however the shadcn cli moves it to / (above src) by default, which causes this issue:

Module not found: Error: You attempted to import ../../@/components/ui/button which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.

@shadcn do you recommend to put @ into src by default (React app here btw), if so should the cli take care of it?

stephanebruckert commented 1 year ago

@vim-diesel also update your components.json with:

"aliases": {
  "components": "src/@/components",
  "utils": "src/@/lib/utils"
}
vim-diesel commented 1 year ago

@stephanebruckert sorry I deleted my comment. I messed up putting the compiler options in before initializing. I deleted the project and tried again and seems to be working okay now.

geekaara commented 1 year ago

I encountered the same issue, my mistake was in pasting "baseUrl": ".", "paths": { "@/*": ["./src/*"] } outside the compilerOptions, please do check

StreetDevs commented 10 months ago

I have the same issue but i tried all the above but is still does not work

lethib commented 9 months ago

@StreetDevs I think that you have the same folder structure as mine:

.
└── src/
    ├── components/
    │   └── ui/
    │       └── button.tsx
    └── lib/
        └── utils.ts

Solved the problem by changing the following line in the button.tsx file:

import { cn } from "@/lib/utils";

by:

import { cn } from "src/lib/utils";

It looks like the @ alias doesn't work (I don't know why though...)

shadcn commented 9 months ago

@lethib What's in your tsconfig.json?

lethib commented 9 months ago

@shadcn My tsconfig.json looks like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    },
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"]
}

In fact, the alias "works": I don't get any direct typescript error (the import of the cn function is accepted by typescript with the alias just above). The error occurs only during the compilation.

theG0ulart commented 8 months ago

@shadcn , that problem occurs only during the compilation when using Create React App + Shadcn-UI

Today I lost a lot of time trying to solve that, but I fail.

@shadcn , any plans to solve that error??

IronHeartDan commented 6 months ago

My mistake was configuring ts config file after npx shadcn-ui@latest init

I resolved by again going through the setup, and this time I setup ts config file first "compilerOptions": { "baseUrl": ".", "paths": { "@/*": [ "./src/*" ] }, }

Then ran npx shadcn-ui@latest init

Incase if it helps someone

efgomes commented 5 months ago

aqqui acontece a mesma coisa, vite com typescript, quando instalo o shadcn ele fica na raiz do projeto, 6 horas tentando resovler isso, vou deixar aqqui e vou continuar usando o antd ate conseguir resolver esses imports

yeongbin-jo commented 5 months ago
yarn run v1.22.21
$ tsc -b && vite build
src/components/ui/button.tsx:5:20 - error TS2307: Cannot find module '@/lib/utils' or its corresponding type declarations.

Still same issue occured :sob: I edited tsconfig.json and vite.config.ts both as document.

yeongbin-jo commented 5 months ago

I solved this problem by adding baseUrl, paths config to tsconfig.app.json.

Like below:

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

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": ["src"]
}
dandheedge commented 5 months ago

I solved this problem by adding baseUrl, paths config to tsconfig.app.json.

Like below:

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

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": ["src"]
}

This works for me. Thank you 👍

KaziMdMehediHasan commented 5 months ago

I solved this problem by adding baseUrl, paths config to tsconfig.app.json.

Like below:

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

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": ["src"]
}

It's finally working for me!

efgomes commented 5 months ago

I solved this problem by adding baseUrl, paths config to tsconfig.app.json.

Like below:

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

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": ["src"]
}

fiz seguindo o codigo do colega, mas como podem ver na iamgem abaixo ele continua sendo criado o @na raiz image

deno-source commented 3 months ago
"baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }

终于找到了一个靠谱的答案

sovetski commented 1 month ago

Thanks @yeongbin-jo, I was editing all tsconfig files except tsconfig.app.json, I don't understand why we have a lot of tsconfig files but anyway you saved my day

Govinda088 commented 1 month ago

I solved this problem by adding baseUrl, paths config to tsconfig.app.json.

Like below:

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

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": ["src"]
}

You are GodSend.

Quinlivanner commented 1 month ago

我通过将baseUrlpaths config 添加到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,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }
  },
  "include": ["src"]
}

Worked for me!!amazing!

Noudea commented 1 month ago

When using shadcn/ui, you might encounter import issues due to path aliasing configuration.

For example if you have this kind of structure

Project Structure

.
└── src/
    ├── components/
    │   └── ui/
    │       └── button.tsx
    └── lib/
        └── utils.ts

Required Configuration

You need to configure both vite.config.ts and tsconfig.json:

// vite.config.ts
resolve: {
  alias: [
    { find: '@', replacement: path.resolve(__dirname, 'src') },
    { find: '@libs', replacement: path.resolve(__dirname, 'src/lib') }
  ]
}
// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"],
      "@libs/*": ["./src/lib/*"]
    }
  }
}

This will allow imports like:

import { cn } from "@libs/utils"

Key Points

Make sure paths match exactly in both files You could have to restart TS Server after config changes

How to Restart TypeScript Server

Using Command Palette

  1. Cmd/Ctrl + Shift + P
  2. Type "TypeScript: Restart TS Server"
  3. Press Enter
Govinda088 commented 1 month ago

When using shadcn/ui, you might encounter import issues due to path aliasing configuration.

For example if you have this kind of structure

Project Structure

.
└── src/
    ├── components/
    │   └── ui/
    │       └── button.tsx
    └── lib/
        └── utils.ts

Required Configuration

You need to configure both vite.config.ts and tsconfig.json:

// vite.config.ts
resolve: {
  alias: [
    { find: '@', replacement: path.resolve(__dirname, 'src') },
    { find: '@libs', replacement: path.resolve(__dirname, 'src/lib') }
  ]
}
// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"],
      "@libs/*": ["./src/lib/*"]
    }
  }
}

This will allow imports like:

import { cn } from "@libs/utils"

Key Points

Make sure paths match exactly in both files You could have to restart TS Server after config changes

How to Restart TypeScript Server

Using Command Palette

  1. Cmd/Ctrl + Shift + P
  2. Type "TypeScript: Restart TS Server"
  3. Press Enter

Thank you very much for the information it will a great help for someone in future.

1saifj commented 1 month ago

after changing the ts.config sometimes you need to exit VsCode or the editor, and if you running VsCode its worth trying restarting the TS server.