Sling Academy
Home/Next.js/Fixing Next.js Error: Use Image from ‘next/image’ Instead of ‘img’

Fixing Next.js Error: Use Image from ‘next/image’ Instead of ‘img’

Last updated: January 01, 2024

Understanding the Error

When you’re developing a project with Next.js and you come across the error message ‘Next.js Error: Do not use <img>. Use Image from ‘next/image’ instead,’ this is Next.js nudging you towards its built-in image optimization feature. Next.js prefers you use their Image component from the ‘next/image’ import because it includes performance optimizations like lazy loading, image resizing, and support for modern image formats.

Integrating the Image Component

To solve the error, you’ll need to swap out the standard HTML <img> tag for the next/Image component provided by Next.js. This means you must import the Image component from ‘next/image’ and use it to display your images. Next.js will then handle the optimization automatically.

Updating Existing Images

If you have existing <img> tags in your code, you’ll need to refactor them to use the Next.js Image component. Let’s say you previously had the following code with a standard <img> tag:

<img src="/path/to/image.jpg" alt="Description" width="500" height="300" />

You would update it to:

import Image from 'next/image';

function MyComponent() {
  return (
    <Image
      src="/path/to/image.jpg"
      alt="Description"
      width={500}
      height={300}
      layout="responsive"
    />
  );
}

Layout Property

The Next.js Image component also requires a ‘layout’ property. You can set it to ‘responsive’, ‘fill’, ‘fixed’, ‘intrinsic’, or ‘raw’. Choosing the right layout is important for how your image will resize along with its container. The ‘responsive’ option, which changes the size of the image as the viewport changes, is often the most useful.

Running a Complete Example

To better understand how to use the Image component, here’s a simple example you can run in a Next.js project:

import Image from 'next/image';

export default function HomePage() {
  return (
    <div>
      <h1>Welcome to My Next.js Page</h1>
      <Image
        src="/path/to/image.jpg"
        alt="Next.js Logo"
        width={256}
        height={144}
        layout="intrinsic"
      />
    </div>
  );
}

This code showcases a simple Next.js page that includes the optimized Image component to display an image file. Replace ‘/path/to/image.jpg’ with the actual path to your image file.

In summary, by updating your code to use the Next.js Image component, you’re not only complying with Next.js’ recommendation but also drastically improving your application’s performance and user experience.

Next Article: Solving Next.js Vercel Deployment ‘Module Not Found’ Error

Previous Article: Fixing Next.js Parsing Error: Cannot Find Module ‘next/babel’

Series: Common Errors in Next.js

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported