Nuxt images with a dynamic :src show broken or 404
Dynamic image works with a fixed path but breaks when :src uses a variableShort answer
Nuxt and Vite rewrite only static asset paths at build time. A :src built from a variable, such as ~/assets/img/${name}.png, is opaque to them, so it is never bundled. Put the files in public and use a root path, or import them with a literal path, a variable import or import.meta.glob.
Fix it fast
1Find out which cause you have
Open the browser network tab and read the failing image URL. A URL that still contains ~/assets means the path was never rewritten.
grep -rn ":src=" app components pages 2>/dev/null2Match what you see, then jump to its fix
What does the error look like?
Dynamic image works with a fixed path but breaks when :src uses a variableImage with a dynamic :src does not load after nuxt build
Cause 1 of 2
A ~/assets path inside a bound :src
1Check
Look for :src with a template string that starts with ~/assets. The build tool rewrites literal src values only.
grep -rn "~/assets" app components pages 2>/dev/null2Fix
Import the images through Vite. For a known set, use import.meta.glob with the url query and look the file up by name.
<script setup lang="ts">
const props = defineProps<{ name: string }>()
const images = import.meta.glob<string>('./assets/img/*.{png,jpg,svg}', {
query: '?url',
import: 'default',
})
const src = await images[`./assets/img/${props.name}.png`]?.()
</script>
<template>
<img :src="src" alt="">
</template>3Verify
Run a production build and open the page.
npx nuxt build
npx nuxt previewConfirmed by nuxt.com
Cause 2 of 2
The images should live in public instead
1Check
Files in public are served from the root with their original names. Files in assets are not served at a static URL.
ls public/img2Fix
Move the files into public and build the URL with a root path.
const imageUrl = computed(() => `/img/${props.name}.png`)3Verify
Request the file straight from the running site.
curl -I http://localhost:3000/img/logo.pngConfirmed by nuxt.com
How do I stop it coming back?
- Use static paths in templates whenever the file is known at build time.
- For runtime names, pick one pattern (public, glob or variable import) and use it everywhere.
- Do not use new URL(..., import.meta.url) in server-rendered code; Nuxt documents that it does not work with SSR.
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
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 fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix