🔥 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: Astro Expected Image Options

Fix the Astro expected image options error: getImage() needs an options object like { src, width }, not a string, undefined or a bare import. Examples for each case.

#Astro#Errors#Images#getImage

“Expected image options.” means getImage() was called with something other than an object. The message shows what it got: “Expected getImage() parameter to be an object. Received undefined.” The fix is to always pass an options object with the image in src: getImage({ src: hero, width: 800 }). Passing a URL string, an imported image on its own, or a variable that turned out to be undefined all trigger it or a close relative.

Key Takeaways

  • getImage() takes one argument: an object with at least src.
  • getImage('/hero.jpg') or getImage(url) fails; wrap it as getImage({ src: url, ... }).
  • getImage(hero) with an imported image gives the sibling error “Expected image options, not an ESM-imported image.”
  • getImage() runs on the server only, in frontmatter or endpoints.

What triggers it

import { getImage } from 'astro:assets';

await getImage();                                 // Received `undefined`
await getImage('https://example.com/a.jpg');      // Received `"https://example.com/a.jpg"`
await getImage(options);                          // options is undefined

The “Received” value is serialized with JSON.stringify, so a string shows up in quotes. That tells you immediately whether you passed a string, undefined or null.

The fix

Pass an object. For an imported local image:

---
// src/pages/index.astro
import { getImage } from 'astro:assets';
import hero from '../assets/hero.jpg';

const heroBg = await getImage({ src: hero, width: 1600, format: 'webp' });
---
<section class="hero" style={`background-image: url(${heroBg.src})`}>
  <h1>Northline Studio</h1>
</section>

For a remote image, include dimensions or inferSize, and make sure the domain is allowed in image.domains:

const avatar = await getImage({
  src: 'https://images.example.com/team/ana.jpg',
  width: 160,
  height: 160,
  format: 'avif',
});

The result has src, attributes and srcSet, which you can spread into a plain <img> or use in CSS. The remote image not allowed fix covers the domain setting.

The sibling error: a bare import

import hero from '../assets/hero.jpg';
await getImage(hero); // Expected image options, not an ESM-imported image.

Astro’s hint for that one is exact: “Try changing getImage(myImage) to getImage({ src: myImage })”.

When options come from props

A helper that builds options from props can pass undefined when a prop is missing:

// src/lib/og.ts
import { getImage } from 'astro:assets';
import type { ImageMetadata } from 'astro';

export async function ogImage(src?: ImageMetadata) {
  if (!src) return undefined;
  return getImage({ src, width: 1200, height: 630, format: 'png' });
}

Guarding before the call keeps a missing image from failing the whole page. If src itself is the problem, see expected src to be an image.

Server only

getImage() can’t run in the browser; calling it from a client component throws a different error, covered in getImage is server only. The Astro image component guide explains when to use getImage() instead of <Image />, our styling docs show background images in Komal Astro themes, and the official getImage() reference lists every option.

FAQ

What does getImage() return in Astro?

An object with the optimized src URL, attributes such as width and height, srcSet data, and the options used. Use it for CSS backgrounds, Open Graph images or custom <img> markup.

Can I pass a URL string to getImage()?

Only inside the options object: getImage({ src: 'https://...', width, height }). A bare string triggers this error.

Why use getImage() instead of the Image component?

Use getImage() when you need the image URL rather than an <img> element, for example a CSS background, a meta tag, or an API response.