question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue asset images not loading

See original GitHub issue
  • Laravel Mix Version: 6.0.9
  • Node Version: 15.5.1
  • NPM Version: 7.3.0
  • OS: Mac OS Big Sur 11.1

Description:

After upgrading to Laravel Mix 6 my build runs successfully but images referenced from the assets folder no longer are loading.

Steps To Reproduce:

Running an existing Laravel v8.21.0 project with Laravel/UI Vue auth scaffolding, after upgrading Laravel Mix to version 6 images referenced from the assets folder in Vue don’t err out on build but display as broken images in the browser.

Referencing images as such:

<img src="../assets/logo.png" alt="Logo">

When I inspect the image I see this:

<img src="[object Module]" alt="Logo">

Any help would be greatly appreciated. Thanks!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:16

github_iconTop GitHub Comments

22reactions
tinyflycommented, Jan 22, 2021

Apparently its a bug with vue-loader which Mix uses. Here is my workaround solution. This only sets esModule to false on the images file-loader.

mix.override(webpackConfig => {
  // BUG: vue-loader doesn't handle file-loader's default esModule:true setting properly causing
  // <img src="[object module]" /> to be output from vue templates.
  // WORKAROUND: Override mixs and turn off esModule support on images.
  // FIX: When vue-loader fixes their bug AND laravel-mix updates to the fixed version
  // this can be removed
  webpackConfig.module.rules.forEach(rule => {
    if (rule.test.toString() === '/(\\.(png|jpe?g|gif|webp)$|^((?!font).)*\\.svg$)/') {
      if (Array.isArray(rule.use)) {
        rule.use.forEach(ruleUse => {
          if (ruleUse.loader === 'file-loader') {
            ruleUse.options.esModule = false;
          }
        });
      }
    }
  });
});
12reactions
fylzerocommented, Jan 12, 2021

@Aiwings Thanks for the response.

I was also able to get the image loaded by doing this:

<img :src="require('./assets/logo.png').default" alt="Logo">

Both of these solutions feel kinda ugly to me tbh.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue assets image not load - Stack Overflow
First you have to bind your image path to the src property and then you have to wrap the path in require('@/assets/profile.jpg') to...
Read more >
Adding static images to a component, they do not appear
Files in the assets folder are not copied across to the build output directly. They undergo tree-shaking so you need an import or...
Read more >
Image asset within component not displaying - Laracasts
Image asset within component not displaying. I have a component.vue file that looks like such: Copy Code <div class="category-container__item-row" ...
Read more >
Error loading image - Material Design for Bootstrap
The image I'm trying to load is in the assets folder and the vue file is in the components folder. The error is...
Read more >
Images, fonts, and assets - Storybook - JS.ORG
In this case, you need to have all your images and media files with relative paths. Otherwise, the browser cannot locate those files....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found