Skip to content
>_VibeUnstuck

Failed to resolve import "@/components/ui/button"

Last verified:

The fix

Failed to resolve import "@/components/ui/button" from "src/App.tsx". Does the file exist? Vite never got the @ alias. TypeScript says Cannot find module '@/components/ui/button' or its corresponding type declarations.

  1. 1

    Aim the alias at the folder that has the files

    Lovable and Bolt keep source in src/, so @/* maps to ./src/*. A Next app whose app/ directory is at the repo root usually maps @/* to ./*. baseUrl has to be . when you use paths. Change the target if your files are in the other place. Do not point it at a folder you wish existed.

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

    Give Vite the same folder as an absolute path

    Next.js does not need a second config. Vite does. The alias value has to be an absolute path. A relative "./src" is used as-is and does not land on the project. "/src" is the root of the disk. If vite.config.ts throws __dirname is not defined, use the fileURLToPath lines below. Restart the dev server after the config changes. If this Vite version has resolve.tsconfigPaths, setting it to true reads paths instead of repeating them. Leave it off when the alias below already matches.

    import path from "node:path";
    import { fileURLToPath } from "node:url";
    import { defineConfig } from "vite";
    
    const root = path.dirname(fileURLToPath(import.meta.url));
    
    export default defineConfig({
      resolve: {
        alias: {
          "@": path.resolve(root, "src"),
        },
      },
    });
  3. 3

    Check one import against the disk

    After the restart, open the path in the error with src/ in place of @/. If that file is there, the alias was the bug. If it is not, stop editing vite.config. The import names a file the project does not have. That is the missing-module page.

  4. 4

    Keep a single alias

    Pick @/. A mix of @/, ~/, and src/ is how one tool resolves an import and the other prints Failed to resolve import. Do not install vite-tsconfig-paths on top of an alias that already points at src, and do not rewrite every import to a long relative path to get past one missing line in the config.

Why it happens

Vite's overlay prints Failed to resolve import "@/components/ui/button" from "src/App.tsx". Does the file exist? The same miss in the editor is TypeScript ts(2307): Cannot find module '@/components/ui/button' or its corresponding type declarations. paths in tsconfig.json only tells TypeScript where to look. Next.js reads those paths when it bundles. Vite does not, unless resolve.alias points @ at the same folder. resolve.tsconfigPaths can read paths for you. It is off by default, and only exists on Vite versions that include the option. Lovable and Bolt are Vite apps. A snippet full of @/components/... imports fails in the preview until that alias exists and aims at the real directory.

The file has to be on disk too. Vite uses this sentence when the alias is wrong and when the file was never written. Open the folder @/ should mean before you edit the config. If src/components/ui/button.tsx is not there, this is a missing module. A paths target of ./src/* resolves nothing when the files live next to app/ at the repo root, and the reverse fails when Lovable's files are in src/. The editor and the bundler can also be reading two different configs.

The next error you'll probably hit

Once this is fixed, the usual follow-up is Module not found: Can't resolve. Module not found: Can't resolve means the import path or package is not on disk, often after an AI edit. Fix the path or install the package, then restart.

Prompt to paste back into your AI tool

Paste this above the error.

Vite throws: Failed to resolve import "@/components/ui/button" from "<file>". Does the file exist?
TypeScript, if it also fails, says: Cannot find module '@/components/ui/button' or its corresponding type declarations.

The file on disk at the alias target is <present or missing>. This project is <Lovable/Bolt Vite, or Next.js>.

Do not rewrite every @/ import to a relative path. Do not add a second alias. Do not install a plugin if resolve.alias can point at the folder. Do not create an empty button.tsx if the import is a mistake.

1. If the file is missing, say so and stop. Do not edit the alias to hide a file that was never written.
2. If the file is there, set tsconfig baseUrl and paths so @/* points at that folder (./src/* on Lovable and Bolt).
3. On Vite, set resolve.alias to the absolute path of the same folder. Next.js does not need a Vite alias.
4. Tell me to restart the dev server. Show only the config diff.

Common questions

+The editor opens the file and the preview says Failed to resolve import. Why?

The editor is using tsconfig paths. Vite has no resolve.alias for @, or the alias points at a different folder than paths. Set the alias to that folder as an absolute path and restart. Next.js reads paths itself and does not use vite.config.

+Cannot find module '@/components/ui/button' or its corresponding type declarations, and the file exists. Why?

baseUrl is missing, paths points at the wrong directory, or the editor is using a different tsconfig than the one you edited. paths are relative to baseUrl. On a Vite app, fixing tsconfig does not fix the preview until resolve.alias matches.

+Does the file exist? is in the error and the file is gone. Is the alias the fix?

No. Vite prints Failed to resolve import and Does the file exist? for a bad alias and for a path that is not on disk. If src/components/ui/button.tsx is missing, create that file or remove the import. Changing vite.config will not invent it.

+I set resolve.tsconfigPaths and it still fails. Why?

The option is off unless you set it to true, and older Vite builds do not have it. The file that imports @/ also has to be included by that tsconfig. An absolute resolve.alias works on current Lovable Vite configs without the flag. Restart after either change.

Related errors