A local image in a Next.js project returns 404 or will not render
GET /public/images/logo.png 404 (Not Found)Short answer
Files in public are served from the site root, so public/avatars/me.png is /avatars/me.png. Never write public in the URL. With next/image, set both width and height unless you import the file or use fill. Images from another domain also need a remotePatterns entry.
Fix it fast
1Find out which cause you have
Open the image URL straight in the browser or with curl. A 404 means the path is wrong; a 200 means the problem is in how the component uses it.
curl -I http://localhost:3000/images/logo.png2Match what you see, then jump to its fix
- 1
The network tab shows a 404 for a URL that starts with /public/.The path includes the public folder nameFix - 2
The page shows an empty box where the image should be, and the Image has no width, height or fill.next/image has no size and the image is not importedFix - 3
The component is given a full https URL and the optimiser rejects it with a 400, or the unconfigured host error.The image comes from another domain that is not allowedFix
- 1
What does the error look like?
GET /public/images/logo.png 404 (Not Found)The image area is blank or collapsed on the pagenext/image rejects a full URL because its hostname is not configured
Cause 1 of 3
The path includes the public folder name
1Check
Next.js serves the files of public starting from the base URL, so the folder name is not part of the address.
ls public/images/
grep -rn "/public/" app/ components/2Fix
Drop public from the path and start it with a slash. A file at public/images/logo.png is requested as /images/logo.png.
import Image from 'next/image'
export default function Logo() {
return <Image src="/images/logo.png" alt="Logo" width={120} height={40} />
}3Verify
The direct URL returns 200.
curl -I http://localhost:3000/images/logo.pngConfirmed by nextjs.org
Cause 2 of 3
next/image has no size and the image is not imported
1Check
next/image needs width and height to reserve space, unless the image is statically imported or the fill property is set.
grep -rn "<Image" app/ components/2Fix
Give the real pixel size, import the file, or use fill inside a positioned parent.
import Image from 'next/image'
import profile from './profile.png'
// static import: size is read from the file
<Image src={profile} alt="Profile" />
// fill: the parent must be position: relative
<div style={{ position: 'relative', height: 300 }}>
<Image src="/profile.png" alt="Profile" fill />
</div>3Verify
Reload and check that the image takes up the space.
npm run devConfirmed by nextjs.org
Cause 3 of 3
The image comes from another domain that is not allowed
1Check
Each part of the src (protocol, hostname, port, path) is matched exactly against images.remotePatterns. A www or subdomain, or a path that is too narrow, will fail.
grep -n "remotePatterns" -A8 next.config.*2Fix
Add the exact host and a path pattern to next.config.
// next.config.js
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'assets.example.com',
port: '',
pathname: '/account123/**',
},
],
},
}3Verify
Restart the dev server and reload the page.
npm run devConfirmed by nextjs.org
How do I stop it coming back?
- Write every public asset path from the root, starting with a slash, and never include the folder name.
- Import images that live next to the component, so a wrong path fails at build time.
- Keep one remotePatterns list and add the real subdomain each time a new image host is used.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
next/image shows nothing for an image from another domain
next/image shows a broken image only for external src values
next/image only optimizes remote images that match an entry in images.remotePatterns in next.config.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fix