How to Pull in Images from Shopify Database - src Error

15 replies
Last updated: Jun 30, 2022
Im trying to display an image like this
<Image src={product.images} width="200" height="200"objectFit="contain" alt=""/>
and it tells me this

Error: Image is missing required "src" property. Make sure you pass "src" in props to the `next/image` component. 
but the src is in there? what am I missing?
AI Update

The error you're seeing happens because product.images is likely a Sanity image object (containing metadata like _type, asset, etc.), not a plain URL string that Next.js Image expects.

Sanity stores images as structured objects, so you need to convert them to URLs first using the @sanity/image-url package.

Here's how to fix it:

1. Install the image URL builder:

npm install @sanity/image-url

2. Create a helper function (e.g., in lib/image.js or lib/sanity.js):

import imageUrlBuilder from '@sanity/image-url'
import { client } from './sanity' // your Sanity client

const builder = imageUrlBuilder(client)

export function urlFor(source) {
  return builder.image(source)
}

3. Use it in your component:

import { urlFor } from '@/lib/image'

<Image 
  src={urlFor(product.images).width(200).height(200).url()} 
  width={200} 
  height={200}
  objectFit="contain" 
  alt=""
/>

4. Configure Next.js to allow Sanity's CDN (in next.config.js):

module.exports = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'cdn.sanity.io',
      },
    ],
  },
}

The key is that urlFor(product.images).url() converts the Sanity image object into an actual URL string. You can also chain other transformations like .width(), .height(), .format('webp'), or .auto('format') for automatic format optimization based on browser support.

If product.images is an array of images, you'd need to access a specific one like urlFor(product.images[0]).url().

Did you resolve the image before passing it to the Image component?
“image”: product.image-&gt;
it's coming from a shopify data base all the other stuf works fine but when using image it wont
So the database has a image link from sanity?
what happens if you try to console.log
product.images
no its not from sanity its from shopify
let me test it
console.log the product.image to see the url
oooh I see now it returns a lot of arrays
how do i get down there and show only 1 of them ?
product.images[0].images[0]
☝️ if there are urls in the images array
got it ty
this is the result
<Image src={product.images[0]}/>
Cool

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?