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

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.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 35

Most upvoted comments

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"]
}

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

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 👍

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

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

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

@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.

@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…)

Reference: https://stackoverflow.com/a/75121301/8345133 In the tsconfig.app.json file, add "extends": "./tsconfig.json" as shown below:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    // ...
   }
}

This worked for me.

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.

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

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

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

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"]
}

Thanks, this worked for me 😃

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!

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

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.

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

@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.

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?