Import image in Next.js (original) (raw)

Last Updated : 10 Mar, 2026

Importing images in Next.js allows developers to optimize images automatically using the built-in next/image component. It improves performance through features like automatic resizing, lazy loading, and modern formats.

Properties of Image Component

The image components support the following props:

**1. src (required): This property accepts a path string, an external URL, or a locally imported image.

**2. width (required): It represents the image's rendered or original width in pixels. For locally imported images, this property is optional.

**3. height (required): It represents the rendered height or the image's original height in pixels. For locally imported images, this property is optional.

**4. layout: It determines the behavior of the image size when the viewport width changes. It supports the following values:

Working with Image component

Run the following command to create a new Next.js project.

npx create-next-app@latest gfg

Project Structure

We'll first clean up some boilerplate code in the pages/index.js (Home Page) and import the Image component.

pages/index.js `

import Image from "next/image";

const HomePage = () => { return ( <> {/* This is a local import, so the height and width props are optional */}

        {/* This image is also served from public 
        directory but using the static path */}
        <div>
            <Image
                src="

https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210420155809/gfg-new-logo.png" alt="GFG logo served with static path of public directory" height="100" width="400" />

        {/* This image is being served from an 
        external URL, for this URL to work we 
        will need to add the hostname 
        'media.geeksforgeeks.org' to our 
        next.config.js file */}
        <div>
            <Image
                src="

https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210420155809/gfg-new-logo.png" height="100" width="400" alt="GFG logo served from external URL" /> </> ); };

export default HomePage;

/next.config.js

/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, images : { domains : ['media.geeksforgeeks.org'] } }

module.exports = nextConfig

`

Step to run the application

Run your Next app using the following command:

npm run dev

Output