Maaz Zindani

Loading the index…

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

Images from a computed path break after vite build in Vue

ErrorImage shows in npm run dev but is missing or undefined after npm run build

Short 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.

  • Applies to Vite 7, Vite 8, Vue 3.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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" src
  2. 2Match what you see, then jump to its fix

    1. 1Code like new URL(`../assets/${name}.png`, import.meta.url) or a bare variable as the first argumentThe path is built from a variableFix
    2. 2import.meta.glob(dir + '/*.png') or a variable in the patternThe glob arguments are not literalsFix
    3. 3A plain string like '/src/assets/photo.png' works in dev and 404s in productionThe image is outside the module graph and not in publicFix

What does the error look like?

  • Image shows in npm run dev but is missing or undefined after npm run build
  • The built page requests an image path that is not in dist
  • new 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(" src

2Fix

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 preview

Confirmed by vite.dev

Did this fix it?

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" src

2Fix

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

Did this fix it?

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 public

2Fix

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 preview

Confirmed by vite.dev

Did this fix it?

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 fix

Web 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

Next step

Want this handled for good?

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