Images work in vite dev but are missing after vite build
GET /assets/hero.png 404 (Not Found) after vite buildShort 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.
Fix it fast
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" \)2Match what you see, then jump to its fix
- 1
Works 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
The 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
Nothing from public is in dist, not even robots.txtpublicDir is turned off, or copyPublicDir is falseFix - 4
The 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
- 1
What does the error look like?
GET /assets/hero.png 404 (Not Found) after vite buildImages 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=*.vue2Fix
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 heroConfirmed by vite.dev
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 dist2Fix
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.pngConfirmed by vite.dev
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 distConfirmed by vite.dev
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 | head2Fix
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/assetsConfirmed by vite.dev
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 fixWeb 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 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
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix