Astro Component Import Error Fix
In this tutorial, you'll learn about Astro Component Import Error Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Astro throws Cannot find module '@/components/Header' or Component is not a constructor when importing .astro files or framework components like React and Vue.
Quick Fix
Step 1: Include the .astro extension in imports
---
// Wrong — missing extension
import Header from '../components/Header';
// Right
import Header from '../components/Header.astro';
---
Expected output: Astro resolves the component file correctly.
Step 2: Check the import path relative to the file
---
// Wrong — incorrect relative path
import Header from '../../components/Header.astro';
// Right — correct path from this file
import Header from '../components/Header.astro';
---
Expected output: The import path matches the file system location.
Step 3: Install and configure framework renderers
npm install @astrojs/react @astrojs/vue @astrojs/svelte
// astro.config.mjs — Wrong: missing integrations
export default defineConfig({ });
// Right
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';
import vue from '@astrojs/vue';
export default defineConfig({
integrations: [react(), vue()],
});
Expected output: React and Vue components are rendered in Astro.
Step 4: Use the correct client directive for interactive components
---
import Counter from '../components/Counter.jsx';
---
<!-- Wrong — component is static HTML -->
<Counter />
<!-- Right — hydrate on load -->
<Counter client:load />
<!-- Or hydrate when visible -->
<Counter client:visible />
Expected output: The component hydrates and becomes interactive on the client.
Step 5: Verify files are in the src directory
Astro only processes files in src/. Move components into src/components/:
project/
├── src/
│ ├── components/
│ │ └── Header.astro
│ └── pages/
│ └── index.astro
├── astro.config.mjs
└── package.json
Expected output: All Astro files are within the src/ directory.
Step 6: Check for TypeScript import issues
---
// Wrong — importing .ts file without extension
import { getData } from '../lib/data';
// Right
import { getData } from '../lib/data.ts';
---
Expected output: TypeScript files are imported with extensions.
Step 7: Restart the Astro dev server
# After adding new integrations or components
Ctrl+C to stop, then:
npm run dev
Expected output: Astro picks up new components and integrations.
Prevention
- Always include
.astroextension in component imports - Install framework integrations before using framework components
- Add
client:*directives for interactive islands - Keep components in
src/components/directory
Common Mistakes with component error
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world ASTRO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro