Fixing the NextJs Error: Invalid src prop on `next/image`, hostname is not configured under images

Let's say we have the following NextJs page:

import Image from 'next/image'
export default function Home() {
  return (
    <main className={styles.main}>
      <h1>The biker cat 😼!</h1>
      <Image
        src="https://www.js-craft.io/wp-content/uploads/2023/06/biker_cat.webp"
        width={500}
        height={413}
      />
    </main>
  )
}

If we try to pass an external url in a NextJs image component, with no other configuration, we will get the below error:

Error: Invalid src prop (https://storage.js-craft.io/biker_cat.webp) on `next/image`, hostname "www.js-craft.io" is not configured under images in your `next.config.js`
See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host

In order to fix this error we need to add the path to the external CDN or image in the next.config.js file:

/** @type {import('next').NextConfig} */
const nextConfig = {
    images: {
        domains: ['www.js-craft.io'],
    },
}

module.exports = nextConfig

In this case www.js-craft.io will be added to the image domains property.

If you are using the create-next-app utility then restarting npm run dev is a good idea to prevent other errors.

And that's it. Here is the link to the Gitub repo used in this example.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

📖 50 Javascript, React and NextJs Projects

Hi friend! Before you go, just wanted to let you know about the 50 Javascript, React and NextJs Projects FREE ebook.

One of the best ways to learn is by doing the work. Choose from 8 project categories and get started right away:

  • Business & Real-World
  • Games & Puzzles
  • Fun & Interesting
  • Tools & Libraries
  • Personal & Portfolio
  • Project Add-Ons
  • Productivity
  • Clones

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects complete with project briefs and wireframes!

Keep building and level up! Get all projects as an ebook right to your inbox!

X