The png image is not displaying in the browser when using background-image with Webpack

I'm currently working with Webpack and attempting to display a png image in my Firefox browser. The issue I'm facing is that when Webpack compiles, it generates two images in the dist folder. One of these images seems to have an error as it won't display on either my laptop or in the browser ("An error occurred while loading the image"). Strangely, Webpack is trying to use this faulty image instead of another one that should work properly.

Looking at my webpack.config.js file, I have the following modules set up:

module: {
    rules: [{
            test: /\.css$/,
            use: ['style-loader', 'css-loader']
        },
        {
            test: /\.(jpe?g|png|gif|svg)$/,
            use: ['file-loader']

        }
    ]
}

In my styles.css file, I have specified the background-image like so:

.logo {
background-image: url('../assets/foto.png');
background-size: cover;
height: 200px;
width: 200px;
margin: 0 auto;}

Within my index.html file, I am utilizing a div container:

<div class="logo"></div>

Here's how my project structure looks like:

 dist/ 
   - h74g3ffgf3ff34h76.png
   - analytics.54t54gg4.js
   - ab0d12j489gh4igh8.png
   - index.html
   - main.h74rg34u73f.js
 src/
     assets/
       - foto.png
     styles/
       - styles.css
   - analytics.js
   - index.html
   - script.js
- package-lock.json
- package.json
- webpack.config.js
     

Versions:

  • Webpack: 5.53.0
  • css-loader: 6.3.0
  • file-loader: 6.2.0

It seems that there might be an issue with the path to the foto.png file, but I haven't been able to pinpoint how to resolve it. Both absolute and relative paths have not worked for me so far.

Why does Webpack generate two images in the dist folder and favor the one that is defective? What could be causing this unexpected behavior?

Answer №1

After some research and experimentation, I resolved the issue by opting for Asset Modules instead of relying on url-loader and file-loader. Here's how the updated code looks like:

{
    test: /\.(jpe?g|png|gif|svg)$/,
    type: 'asset/resource'
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

executing a function within a Vue.js 3 component's <template> tag

As I delved into the world of fetching APIs and displaying their data on a Vue.js3 webpage, I encountered an interesting issue. My goal was to showcase different dog breeds and have a random dog image display when a breed is clicked. However, when I tried ...

manage dynamic form data in React using Ant Design components

My attempts to work with dynamic forms using React and Ant Design have been challenging. Despite my efforts to find solutions online, I have had no luck. Here is a CodePen link where I have recreated the issue I am facing: https://codepen.io/sethen/pen/Rwr ...

Solving the conundrum of sending a Javascript date object (formatted datetime) through Laravel API

When my Laravel 5 API fetches datetime columns from my MySQL database, the JSON output is in this format: 2015-08-13 13:45:00 However, the widget that reads this JSON data expects a JavaScript Date Object. How can I convert my datetime to a JavaScript Da ...

Strategies for distributing a single lit-element instance among multiple web components

With numerous web components stored in individual repositories, I am seeking a way to share a single instance of lit-element/lit-html across all of them in order to optimize bundle size. Rather than having a separate instance in each bundle. To achieve th ...

Error: The function `filter` is not a valid method of `states` object in the `searchStates` function

[ { "abbr": "AL", "name": "Alabama", "web": "www.google.com", "capital": "Montgomery", "lat": "32.361538", "long": "-86.279118" }, { "abbr": "AK", "name": "Alaska", "capita ...

Calculating the total percentage and total number with a specific subject can be done effectively using the reduce method

I have a special function called subjectTotal that calculates the total score for a subject based on multiple choice and true/false questions. I am attempting to incorporate a percentage calculation directly within the subjectTotal function. Additionally, ...

Implementing a fixed top navigation bar with jQuery to override default CSS properties

My latest project involves creating a WordPress site, and I recently found a way to fix my navbar at the top of the page as users scroll down. Here's the code snippet I used: (function( $ ){ var navOffset = jQuery("nav").offset().top; jQu ...

Guide to adding animation to the Bootstrap 4 Dropmenu

There is a collapse animation on the navbar toggler: <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-sca ...

Having trouble eliminating the underline on Vue router-link?

I've experimented with various approaches in an attempt to remove the underline from router-link. This is the code I have: <router-link :to="{name: 'Plan'}"> <div>Plan Your Trip</div> <div class=&apos ...

Issue with external variable not being updated properly in success callback

Working through an array of data, I make updates to a variable called commentBody during each iteration. However, even though the variable is modified within the loop itself, whenever I try to access it from inside a success callback, it consistently show ...

Is your CSS failing to link or display properly?

I'm encountering a frustrating issue with my code that I suspect has a simple fix, but I can't seem to figure it out. It seems like my CSS isn't linking to my HTML file properly. I've searched through various resources for a solution, b ...

Manipulating object information within a nested for loop

I have a variable called jobs in my scope, which is an array containing objects for each department along with their respective data. [ “Accounting”: { “foo” : “foo”, “bar” : "bar" }, “Delivery”: { ...

Screen the array of objects for data that is valid

My array is structured with keys and values like this: const array = [ { DEVICE_SIZE: ['036', '048', '060', '070'], }, { DEVICE_VOLTAGE: ['1', '3'], }, { &a ...

When Gulp is executed it throws an error that is not handled: `error` event is unhandled

Check out the code below: main.js var React = require('react'); var App = require('./components/app.js'); React.render( <App />, document.getElementById('container') ); app.js var React = require(' ...

Having trouble retrieving the value of the second dropdown in a servlet through request.getParameter

I am facing an issue with storing the value of the second dropdown in a servlet after utilizing an ajax call in Java to populate it based on the selection made in the first dropdown. While I was able to successfully store the value of the first dropdown ...

Extracting information from a division using Selenium

I am attempting to choose an element for clicking on a page (). The element in question is a date selection within a calendar pop-up. Specifically, the element is: <div class="day unit in-range" data-time="1698822000000">1</d ...

Transferring parameters from HTML to CSS using Less and addressing two key design problems

I am currently in the process of designing a webpage, and I've encountered three distinct issues - two are related to pure .css, while one is related to less. Firstly, I have set margin: auto and padding: auto on the body tag, but it's not cente ...

Troubles encountered while processing STL file readers

Utilizing STLLoader.js, I am able to render components successfully, however, they all appear with one uniform color which does not resemble real-world components. The image above showcases my implementation in Three.js using STLLoader.js (Binary STL file ...

When my View is deployed, the relative URL path breaks

As I work on my ASP MVC app, I encountered an issue with the path to a Partial View when using Javascript. The path needed to be hardcoded and differs between Development and Production environments. Specifically, in Dev there is no App specification, whe ...

Unit tests in Vue 3 do not utilize configureWebpack in vue.config.js

Within my Vue configuration, the following code snippet can be found: configureWebpack: { resolve: { alias: { react: path.resolve(__dirname, 'composition/react'), hooks: path.resolve(__dirname, 'composition'), }, ...