A Vue image with a dynamic src shows a broken link in a webpack build
404 (Not Found) on the image requestShort answer
webpack only rewrites image paths it can see at build time. A fixed src like ./logo.png is turned into a module request, but a src built from a variable at runtime is left as a plain string and points nowhere. Move the images to the public folder and prefix the path, or import each image so webpack hands you its final URL.
Fix it fast
1Find out which cause you have
Compare the URL in the broken img tag with the files the build produced. If the tag holds the raw path you typed, webpack never processed it.
npx vue-cli-service build ls dist/img2Match what you see, then jump to its fix
- 1
A fixed src works, but :src="item.image" or a template string gives a 404 or a broken iconThe path is computed at runtime, so webpack never saw itFix - 2
The image works in the template but an @/assets path inside script code fails to resolveThe @ alias is used where only templates understand itFix - 3
src="/images/foo.png" returns 404 in the browserAn absolute path is kept as-is and no file exists at that URLFix - 4
webpack cannot process a .png import in your own webpack configAn imported image has no asset rule in a plain webpack setupFix
- 1
What does the error look like?
404 (Not Found) on the image requestBroken image icon where :src is a variable
Cause 1 of 4
The path is computed at runtime, so webpack never saw it
1Check
In a template, a static relative src such as ./image.png is compiled into a require call. A bound value is only a string when the page runs, so nothing is bundled for it.
grep -rn ":src=" src/2Fix
Put the files in the public folder, which is copied as-is and not processed by webpack, and build the URL from the base URL.
// component script
data () {
return { publicPath: process.env.BASE_URL }
}
<!-- template -->
<img :src="`${publicPath}my-image.png`">3Verify
Run the dev server and open the page: the image request returns 200.
curl -I http://localhost:8080/my-image.pngConfirmed by cli.vuejs.org
Cause 2 of 4
The @ alias is used where only templates understand it
1Check
Vue CLI aliases @ to the src folder for templates only. A request written in plain JavaScript needs a path webpack can resolve on its own.
grep -rn "@/assets" src/ --include=*.js --include=*.vue2Fix
In script code, use a relative request such as ./assets/logo.png, or import the image and bind the result.
import logo from "./assets/logo.png";
// data: { logo }
// template: <img :src="logo">3Verify
Rebuild and check that the file appears in dist/img and the page loads it.
npx vue-cli-service build && ls dist/imgConfirmed by cli.vuejs.org
Cause 3 of 4
An absolute path is kept as-is and no file exists at that URL
1Check
A path that starts with / is preserved exactly. webpack does not look it up in src, so the file must exist at that spot in the public folder.
ls public/images2Fix
Either place the file in public/images, or change the path to a relative one (./ or ~) so webpack bundles it.
<img src="./assets/foo.png">3Verify
Reload the page and look at the Network tab.
curl -I http://localhost:8080/images/foo.pngConfirmed by cli.vuejs.org
Cause 4 of 4
An imported image has no asset rule in a plain webpack setup
1Check
In webpack 5, images and fonts are handled by an asset module type, which needs no loader. A config with no such rule has nothing that handles the file.
grep -n "asset" webpack.config.js2Fix
Add an asset rule. With asset/resource, import gives you the emitted file's URL, which you can assign to src.
module: {
rules: [
{ test: /\.(png|jpg|svg)$/, type: "asset/resource" },
],
},3Verify
Rebuild: the import resolves to a URL string.
npx webpack --mode developmentConfirmed by webpack.js.org
How do I stop it coming back?
- Decide per image: fixed and small goes through webpack, a long list chosen at runtime lives in public.
- Never write the same asset path two ways in one project.
- Open the built dist folder once after changing how images are referenced.
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