Skip to content

Astro Component Import Error Fix

DodaTech Updated 2026-06-24 3 min read

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 .astro extension 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

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. 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

### Why does my React component render as plain HTML?

React components in Astro render as static HTML by default. Add a client:* directive like client:load or client:visible to make them interactive. Without a client directive, the component is server-rendered and not hydrated on the client.

What is the difference between .astro and .mdx files?

.astro files are components with a frontmatter script section and HTML template. .mdx files are Markdown with embedded JSX components. Use .astro for layouts and components. Use .mdx for content pages that include interactive components.

Can I use Astro components inside React components?

No. Astro components are server-only and cannot be imported into React components. Use framework-agnostic patterns: pass HTML as slots, create Web Components, or use the Astro island architecture with client:visible for interactive parts.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro