Maaz Zindani

Loading the index…

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

A Vue image with a dynamic src shows a broken link in a webpack build

Error404 (Not Found) on the image request

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

  • Applies to Vue CLI 5, webpack 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1A 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. 2The image works in the template but an @/assets path inside script code fails to resolveThe @ alias is used where only templates understand itFix
    3. 3src="/images/foo.png" returns 404 in the browserAn absolute path is kept as-is and no file exists at that URLFix
    4. 4webpack cannot process a .png import in your own webpack configAn imported image has no asset rule in a plain webpack setupFix

What does the error look like?

  • 404 (Not Found) on the image request
  • Broken 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.png

Confirmed by cli.vuejs.org

Did this fix it?

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=*.vue

2Fix

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/img

Confirmed by cli.vuejs.org

Did this fix it?

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/images

2Fix

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

Confirmed by cli.vuejs.org

Did this fix it?

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

2Fix

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 development

Confirmed by webpack.js.org

Did this fix it?

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 fix

Next step

Want this handled for good?

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