Last Updated : 23 Jul, 2025
Next.js is a full-stack React-based framework. Unlike a traditional react app, which loads and renders the entire application on the client, Next.js allows the first-page load to be rendered by the server, which is great for SEO and performance.
Some of the key features of Next.js are:
You can learn more about Next.js here. In this article, we will learn to import images in Next.js using the Image component.
Image component in Next.js (next/image): Next.js Image component is an evolved form of <img/> element in HTML. It comes built-in with performance optimization which helps in achieving good Core web vitals. This performance boost is factored in Google's ranking algorithm, hence improving the ranking of our website.
The Image component supports the following built-in optimizations:
Properties of Image Component: The image components support the following props:
Working with Image component: Run the following command to create a new Next.js project.
npx create-next-app@latest gfg
Project Structure:
For the scope of this article, we will only focus on the public and pages directory. The public directory contains all the static files that need to be served when the Next.js app is built for deployment.
In this example, we'll create and add three images with different sources, one will be imported from the public directory, the second image will be served through the static path from the public directory and the other one will be served from an external URL. You can add your image to the public directory (Here we've added gfgLogo.png). And for the external URL, we're using this image, but for the external URL to work, we'll have to add its domain name to the next.config.js file to protect our application from malicious users.
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 */}
<div>
<Image src={gfgLogo}
alt="GFG logo imported from public directory" />
</div>
{/* 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"
/>
</div>
{/* 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"
/>
</div>
</>
);
};
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4