Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Images work in vite dev but are missing after vite build

ErrorGET /assets/hero.png 404 (Not Found) after vite build

Short answer

Vite only copies a file into dist when it can see it: an import, a static new URL() or a CSS url(), or a file inside the public folder. An image named at runtime from a plain string, or kept in a folder Vite does not treat as public, is never copied. Import it, or move it to public and use a root path.

  • Applies to Vite 7, Vite 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Build, then list what actually landed in dist. Compare it with the images you expect.

    npx vite build
    find dist -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.svg" -o -name "*.webp" \)
  2. 2Match what you see, then jump to its fix

    1. 1Works in dev, 404 in the build; the code has something like src={'/src/assets/' + name + '.png'}The image path is a runtime string, so Vite never sees the fileFix
    2. 2The file is present in dist, but the browser asks for a different URL and gets 404The file is in public but referenced with a relative or src pathFix
    3. 3Nothing from public is in dist, not even robots.txtpublicDir is turned off, or copyPublicDir is falseFix
    4. 4The image works in the build but there is no separate file; the CSS or JS holds a data: URLSmall images were inlined, not copied as filesFix

What does the error look like?

  • GET /assets/hero.png 404 (Not Found) after vite build
  • Images show in npm run dev but the dist folder has no image files

Cause 1 of 4

The image path is a runtime string, so Vite never sees the file

1Check

Search the source for image paths that are built from variables. Vite only follows imports and new URL() calls whose string part is static.

grep -rn "assets/" src --include=*.js --include=*.jsx --include=*.ts --include=*.tsx --include=*.vue

2Fix

Import the image so it joins the build, or use new URL with a template string that keeps the folder static. For a large set, use import.meta.glob. Note that new URL with a template string does not match files in sub folders.

// single file
import heroUrl from './assets/hero.png'

// name chosen at runtime
function getImageUrl(name) {
  return new URL(`./assets/${name}.png`, import.meta.url).href
}

3Verify

Rebuild and check that the hashed file is now in dist/assets.

npx vite build
ls dist/assets | grep -i hero

Confirmed by vite.dev

Did this fix it?

Cause 2 of 4

The file is in public but referenced with a relative or src path

1Check

Files in public are copied to the root of dist with the same name. They must be referenced from the root, without the word public and without a relative prefix.

ls public
ls dist

2Fix

Reference public files with a root absolute path. Do not import them and do not write ../public/.

// public/logo.png  ->  dist/logo.png
const logo = '/logo.png'

3Verify

Serve the build and request the file directly.

npx vite preview &
curl -I http://localhost:4173/logo.png

Confirmed by vite.dev

Did this fix it?

Cause 3 of 4

publicDir is turned off, or copyPublicDir is false

1Check

Look in the Vite config for publicDir and build.copyPublicDir. By default the public folder is copied to the output folder.

grep -n -E "publicDir|copyPublicDir" vite.config.*

2Fix

Remove the setting, or set publicDir back to the folder that holds your files. Setting publicDir to false switches the feature off.

// vite.config.js
export default defineConfig({
  publicDir: 'public',
  build: { copyPublicDir: true },
})

3Verify

Rebuild and check that public files are in the root of dist.

npx vite build
ls dist

Confirmed by vite.dev

Did this fix it?

Cause 4 of 4

Small images were inlined, not copied as files

1Check

Assets below the inline limit (4096 bytes by default) are written into the bundle as base64 instead of files.

grep -rl "data:image" dist | head

2Fix

This is expected behaviour. If you need a real file, put the image in public, or lower the limit.

// vite.config.js
export default defineConfig({
  build: { assetsInlineLimit: 0 },
})

3Verify

Rebuild and list dist/assets again.

npx vite build
ls dist/assets

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Prefer importing an image to naming it from a string; imports are checked at build time.
  • Keep files that must have a fixed name (favicon, robots.txt, social images) in public and link them with a root path.
  • Run vite build and vite preview before you deploy, not only vite dev.
  • Keep dynamic image folders flat, or use import.meta.glob.

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

A background image in CSS does not load in Vite

Background image or web font referenced in url() does not show

Vite treats url() in CSS like an import: relative paths resolve from the CSS file, absolute paths from the project root during dev, and files in public are served at the site root.

3 causes, each with a fix

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.