🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Astro Fundamentals••By Komal Nakrani

Fix: Could Not Import File in Astro

Astro's 'Could not import file' error means an import path points at nothing. Check typos, letter case, the .astro extension and tsconfig path aliases to fix it.

#Astro#Errors#Imports#Vite

Astro shows “Could not import file” when an import in a component, page or layout points at a path that does not resolve. The usual causes are a typo, wrong letter case, a missing .astro extension, or a path alias that is not defined in tsconfig.json. Fix the path so it matches the real file and the error clears on the next reload.

Key Takeaways

  • The message names the exact import that failed; search your code for it.
  • .astro imports need the extension: ../components/Card.astro.
  • Letter case matters on Linux and in most CI builds, even if macOS ignores it.
  • Aliases like @/components only work if compilerOptions.paths defines them.
  • For packages, the fix is usually npm install.

The error

In astro dev the error overlay shows:

Could not import file.
Could not import `../components/Hedaer.astro`.
Hint: This is often caused by a typo in the import path. Please make sure the file exists.

Astro rewrites Vite’s lower-level “Failed to load url” error into this one, so the path in backticks is exactly what your code asked for.

Why it happens

Vite resolves every import relative to the file that contains it. If nothing exists at that path, the module cannot load. Common versions of the mistake:

  1. Typo or moved file. Hedaer.astro, or a component you moved from src/components/ to src/components/ui/.
  2. Wrong depth. A page in src/pages/blog/ needs ../../components/, not ../components/.
  3. Missing extension. TypeScript files resolve without an extension, .astro files do not.
  4. Case mismatch. card.astro imported as Card.astro works on a case-insensitive macOS disk and fails on Linux.
  5. Undefined alias. @/lib/posts without a matching paths entry.

Fix the path

Check the file really exists where the import points:

# Terminal
ls src/components/
grep -rn "Hedaer" src/

Then correct the import, including the extension:

---
// src/pages/blog/index.astro
import Header from '../../components/Header.astro';
---
<Header />

If you renamed a file only by case, Git on macOS may not record the change. Rename it explicitly so CI sees the new name:

# Terminal
git mv src/components/card.astro src/components/Card.astro

Fix a path alias

Astro reads path aliases from tsconfig.json. Define the alias there:

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

Restart astro dev after changing tsconfig.json. Our TypeScript config guide covers aliases in more detail, and the folder structure docs show where each kind of file usually lives.

Fix a missing package

If the path is a package name (import confetti from 'canvas-confetti'), the package is not installed in this project:

# Terminal
npm install canvas-confetti

After a fresh clone, run npm install before npm run dev. If you started from one of our Astro themes, this is the most common cause on first run. For errors where the file exists but cannot be used in the browser, see fix: module is server-only. The Astro imports reference lists which file types can be imported.

FAQ

Why does my Astro import work locally but fail on deploy?

Almost always letter case. macOS treats Card.astro and card.astro as the same file; Linux build servers do not. Make the import match the file name exactly and commit the rename with git mv.

Do I need the .astro extension in imports?

Yes. Astro components must be imported with .astro. TypeScript and JavaScript files can omit their extension.

Why does my @/ alias not work in Astro?

The alias is not defined in tsconfig.json, or the dev server was not restarted after adding it. Add a paths entry under compilerOptions and restart astro dev.