Fix: Image Not Found in Astro

Error message:
Image not found.
Images & Assets 2025-01-25

What Causes This Error?

This error occurs when Astro cannot find the image file at the specified path. This usually happens due to typos, incorrect paths, or missing files.

The Problem

---
import { Image } from 'astro:assets';
// ❌ File doesn't exist at this path
import heroImage from '../assets/hero.jpg';
---

<Image src={heroImage} alt="Hero" />

The Fix

Verify File Exists

# Check if file exists
ls -la src/assets/

# Common locations for images
src/assets/
public/images/

Correct the Import Path

---
import { Image } from 'astro:assets';

// ✅ Correct path to existing file
import heroImage from '../assets/images/hero.jpg';
---

<Image src={heroImage} alt="Hero" />

Common Scenarios

Case Sensitivity

---
// ❌ Wrong case (file is hero.jpg, not Hero.jpg)
import heroImage from '../assets/Hero.jpg';

// ✅ Match exact filename case
import heroImage from '../assets/hero.jpg';
---

File Extension Mismatch

---
// ❌ Wrong extension (file is .png, not .jpg)
import logo from '../assets/logo.jpg';

// ✅ Correct extension
import logo from '../assets/logo.png';
---

Relative Path Issues

---
// From src/pages/blog/post.astro

// ❌ Wrong relative path
import image from './assets/photo.jpg';

// ✅ Correct path from current file location
import image from '../../assets/photo.jpg';
---

Using Public Directory

---
// Images in public/ don't need imports
// public/images/hero.jpg
---

<!-- ✅ Reference directly with / path -->
<img src="/images/hero.jpg" alt="Hero" />

<!-- Or with Image component (requires dimensions for remote-style) -->
<Image
  src="/images/hero.jpg"
  alt="Hero"
  width={800}
  height={600}
/>

Dynamic Imports with Glob

---
// ✅ Use import.meta.glob for dynamic imports
const images = import.meta.glob('../assets/gallery/*.{jpg,png,webp}');

// Check what's available
console.log(Object.keys(images));
---

Content Collections Images

---
// In content collection, use relative paths from the markdown file
// src/content/blog/my-post.md
---
coverImage: ./images/cover.jpg  # Relative to the markdown file
---

// Or use full path from src
---
coverImage: ~/assets/blog/cover.jpg
---

Verify Path Structure

src/
├── assets/
│   ├── images/
│   │   └── hero.jpg    ← File location
│   └── logo.png
├── pages/
│   └── index.astro     ← Import from here: ../assets/images/hero.jpg

TypeScript Path Aliases

// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@assets/*": ["src/assets/*"]
    }
  }
}
---
// ✅ Use path alias
import heroImage from '@assets/images/hero.jpg';
---

Debugging Missing Images

---
import { Image } from 'astro:assets';

// Log available files
const allImages = import.meta.glob('../assets/**/*.{jpg,png,webp,gif}');
console.log('Available images:', Object.keys(allImages));
---

Quick Checklist

  • Verify file exists at the path
  • Check case sensitivity (Linux is case-sensitive)
  • Verify correct file extension
  • Use correct relative path from current file
  • For public/ images, use / prefix paths