🔥 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)
Performance••By Komal Nakrani

Fix: Expected src to Be an Image in Astro

Fix "Expected src to be an image" in Astro: pass an imported image or remote URL to Image and getImage(), call lazy glob imports, and use image() in schemas.

#Astro#Errors#Images#astro:assets

“Expected src to be an image.” means <Image />, <Picture /> or getImage() received a src that’s neither an imported image nor a URL string. The message shows what arrived: “Expected src property for getImage or <Image /> to be either an ESM imported image or a string with the path of a remote image. Received undefined (type: undefined).” The hint: “This error can often happen because of a wrong path. Make sure the path to your image is correct. If you’re passing an async function, make sure to call and await it.” Usually src is undefined because a prop, frontmatter field or lookup came back empty.

Key Takeaways

  • Read the “Received” value: undefined, an empty string, or a function each point to a different fix.
  • undefined usually means a wrong prop name, a missing frontmatter field, or a failed lookup.
  • Lazy import.meta.glob() returns functions. Call them, or use eager: true.
  • Frontmatter images need image() in the collection schema to become imported images.

Received undefined

The most common case. Trace where src comes from:

---
// src/components/Card.astro
import { Image } from 'astro:assets';
const { cover, title } = Astro.props;
---
<Image src={cover} alt={title} width={640} />

If the parent passes image={...} instead of cover={...}, or the entry has no cover in frontmatter, src is undefined. Check the parent and the data, then guard optional images:

{cover && <Image src={cover} alt={title} width={640} />}

Frontmatter images without image()

A content collection field typed as z.string() gives you a plain path string like "./cover.jpg", which then fails with a different error about local images. Declare it with the schema helper so Astro imports the file for you:

// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

const blog = defineCollection({
  loader: glob({ pattern: '**/*.md', base: './src/content/blog' }),
  schema: ({ image }) =>
    z.object({
      title: z.string(),
      cover: image().optional(),
    }),
});

export const collections = { blog };

Then post.data.cover is image metadata you can pass straight to <Image />. The frontmatter image tip covers paths and alt text.

Received a function

---
import { Image } from 'astro:assets';
const images = import.meta.glob<{ default: ImageMetadata }>('../assets/team/*.jpg');
const { file } = Astro.props;
---
<Image src={images[`../assets/team/${file}`]} alt="" />

Without eager, each glob value is a function that returns a promise. Call it:

<Image src={images[`../assets/team/${file}`]()} alt="" />

Astro awaits a promise passed as src and uses its default export. If the lookup key is wrong, you get undefined instead, so log Object.keys(images) to compare paths. Using { eager: true, import: 'default' } gives you the images directly.

Received an empty string

A CMS field left blank often arrives as "". Treat it like a missing image and render a fallback or nothing.

Using getImage()

getImage() takes an options object with src inside it:

import { getImage } from 'astro:assets';
import hero from '../assets/hero.jpg';

const optimized = await getImage({ src: hero, width: 1200, format: 'webp' });

Passing the image itself, getImage(hero), gives a related error, and passing nothing gives “Expected image options.”; see the expected image options fix.

The Astro image component guide covers props, and the images cheatsheet lists every way to reference an image. Komal Astro themes keep images in src/assets/, as our project structure docs explain. See the official Image reference for all props.

FAQ

Can I pass a local path string to the Astro Image component?

Not a relative path. Import the file (import hero from '../assets/hero.jpg') and pass the import, or put it in public/ and use a plain <img>. Strings are treated as remote URLs or public/ paths.

Why is my frontmatter image undefined?

The field is missing from that entry, misspelled, or not declared with image() in the schema. Check the Markdown file and the collection config.

Does Astro accept a dynamic import as src?

Yes. A promise from import() is awaited and its default export used. A function that returns the promise must be called first.