Images from a computed path break after vite build in Vue
Image shows in npm run dev but is missing or undefined after npm run buildShort answer
Vite only rewrites new URL(path, import.meta.url) when it can read the path at build time. A path built from a variable is left as it is, so the file is never copied or hashed. Use import.meta.glob to map the files, or put the images in public and use root paths.
Fix it fast
1Find out which cause you have
Build the app, then compare the image address the page requests with the files Vite wrote to dist/assets.
npx vite build ls dist/assets | head -n 30 grep -rn "import.meta.url" src2Match what you see, then jump to its fix
- 1
Code like new URL(`../assets/${name}.png`, import.meta.url) or a bare variable as the first argumentThe path is built from a variableFix - 2
import.meta.glob(dir + '/*.png') or a variable in the patternThe glob arguments are not literalsFix - 3
A plain string like '/src/assets/photo.png' works in dev and 404s in productionThe image is outside the module graph and not in publicFix
- 1
What does the error look like?
Image shows in npm run dev but is missing or undefined after npm run buildThe built page requests an image path that is not in distnew URL(..., import.meta.url) gives a wrong address in the production bundle
Cause 1 of 3
The path is built from a variable
1Check
Vite's docs say the URL string must be static so it can be analysed; otherwise the code is left as it is. A template literal with a known folder and extension is handled, but a fully dynamic value is not.
grep -rn "new URL(" src2Fix
Keep the folder and extension literal in the template, or collect the files with import.meta.glob and look the name up.
// map of path -> url, with literal arguments
const images = import.meta.glob('../assets/*.png', {
eager: true,
query: '?url',
import: 'default',
})
export function imageUrl(name) {
return images[`../assets/${name}.png`]
}3Verify
Build and check that the lookup returns a hashed path under /assets.
npx vite build
npx vite previewConfirmed by vite.dev
Cause 2 of 3
The glob arguments are not literals
1Check
Vite requires every argument of import.meta.glob to be a literal. Variables and expressions are not accepted.
grep -rn "import.meta.glob" src2Fix
Write the pattern as a literal string. Use one glob for the whole folder and filter the keys in your code.
const all = import.meta.glob('../assets/**/*.{png,jpg,svg}', {
eager: true,
query: '?url',
import: 'default',
})3Verify
Log the keys once in dev and in the preview build; both should list the same files.
console.log(Object.keys(all))Confirmed by vite.dev
Cause 3 of 3
The image is outside the module graph and not in public
1Check
Only imported assets are bundled. Files in public are copied to the root of dist as they are, and Vite tells you to reference them by root absolute path.
grep -rn "/src/assets" src
ls public2Fix
Either import the file so Vite bundles it, or move it to public and use a root path such as /images/photo.png.
<script setup>
import photo from './assets/photo.png'
</script>
<template>
<img :src="photo" alt="photo" />
</template>3Verify
Build, serve dist with the preview server and load the page.
npx vite previewConfirmed by vite.dev
How do I stop it coming back?
- Test the production build with vite preview before you deploy, not only the dev server.
- Keep a literal folder and extension in every dynamic asset path.
- Use public only for files that must keep their exact name.
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.
Related fixes
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 fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
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
Apollo Client is blocked by CORS when the GraphQL server runs on AWS Lambda
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apollo Server does not set CORS for you when it runs behind a framework or integration, and on Lambda that layer is API Gateway.
5 causes, each with a fixWeb apps
A POST to a Google Apps Script web app returns 405 or a CORS error
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.
3 causes, each with a fix