Troubleshooting webpack issue: CSS background image fails to load

After reading numerous posts regarding CSS background images not loading, I believe my situation is unique.

Here is how I've configured my webpack setup (relevant parts only):


{
    test: /\.(png|svg|jpg|gif)$/,
    use: [
      {
        loader: 'file-loader',
      }
    ]
  },
  {
    test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
    use: [
      {
        loader: 'file-loader',
      }
    ]
  },
  {
    test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
    use: [
      {
        loader: 'url-loader',
      }
    ]
  },
  {
    test: /\.less$/,
    use: [
      {
        loader: "style-loader"
      },
      {
        loader: "css-loader"
      },
      {
        loader: "less-loader",
        options: {
          includePaths: [
            path.resolve(__dirname, "assets", "less")
          ]
        },
      }
    ],
    exclude: /node_modules/
  }

I have set my public path like this:


output: {
path: path.resolve(__dirname, "public"),
filename: "bundle.js",
publicPath: "public/"
},

When I build the webpack file, everything is moved to the /public directory correctly and styles are rewritten properly. Even though I can manually locate the image that is not loading in the public directory, one particular style appears messed up:

.section-intro {
background: url(public/76d7fa525cc5b3d641b9f774b7a79c92.png) center center no-repeat;
background-size: cover;
}

I suspected that it wasn't being set correctly, but hovering over the style in Chrome seems to indicate otherwise - it displays

localhost:8080/public/76d7fa525cc5b3d641b9f774b7a79c92.png
.

Other elements, like fonts, load fine. However, when I manually try to access

localhost:8080/public/76d7fa525cc5b3d641b9f774b7a79c92.png
, it gives me a 404 error, which confuses me.

In my express server, I have this line:

this.app.use(express.static(path.resolve(__dirname, "..", "public")));

This should allow me to view the images, right?

I'm unsure whether this issue lies on the Express or Webpack side, but it's causing quite a bit of frustration. Any help would be greatly appreciated.

Thank you!

Answer №1

I hesitate to label this as a definitive solution, so I will refrain from marking my response as correct until it has been validated by someone else.

However, the issue was resolved by replacing file-loader with url-loader for the png matching criteria. While this may address the issue to some extent, I am not entirely convinced that it completely resolves the underlying problem. The main distinction is that url-loader is capable of generating a data URL if the file size is small enough. Although this rectifies the issue for smaller images, I anticipate encountering difficulties again if a larger image is added.

I would greatly appreciate a more comprehensive resolution to this dilemma.

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

"The application functions properly with app.all, but encounters issues with app.use. The reason for this is currently

I'm facing some challenges while attempting to integrate middleware into my application. Specifically, the app.use() function doesn't seem to be working as expected and I'm unsure of the reason behind it. Below is a snippet of what I cur ...

Organizing content on a webpage with specific pixel measurements

I have a div and am utilizing Bootstrap 5. I'd like to have the Description and Length elements on separate lines when the page width is <=1024px. <div class="col-3 col-md-4 col-lg-3"> <div class="card d- ...

Tips for dividing a vue.js template into a .vue file while keeping the code in .js

I am trying to organize my Vue.js template by separating it into a .vue file, while keeping the code in a .js file. Here is an example of a Vue "single file component": <template> .. </template> <script> .. </script> Current Sol ...

I am looking to create a layout with two images placed next to each other using bootstrap, regardless of their dimensions. However, the images do not automatically adjust themselves within

<div class="container" style="background-color: aqua;"> <div class="row"> <div class="col-md-6"> <img src="nature.jpeg" class="img-responsive"> </div> <div class="col-md-6"> ...

Encountering issues loading background image with Reactjs and Webpack

I've encountered an issue while trying to load a background image in my React project from a custom CSS file. I am currently using Webpack 4. .bgimage { height: 100vh; background: url('../images/header.jpg'); } Unfortunately, the image ...

What is the method to display a value in an input field using ExpressJS?

Currently, I am delving into ExpressJS and have bootstrapped an application. While working on a basic login feature, I encountered a scenario where the user would enter the correct email but an incorrect password. In such cases, an error message for ' ...

Highlight the text element that has been clicked

I'm looking to create a list of text elements that can be underlined when clicked individually. Adding the text decoration to the tabText applies it to all items, but I need a way to remove the underline from the previously clicked tab when clicking o ...

Angular2: The '@Component' decorator does not contain a 'directives' property in its configuration object

After creating a directive to auto-expand a textbox, I encountered an error when implementing it into the component. myAppComps.ts https://i.sstatic.net/rZHQc.png NPM RUN BUILD https://i.sstatic.net/DDY4k.png auto-grow.directives.ts https://i.sstat ...

The <div> element is not compatible with the <canvas> element for use

I'm having trouble inserting a <canvas> animation within a <div> element. The current code displays the animation across the entire page, but I would like to contain it within a single section, like this: <div><canvas> </can ...

Navigating routes with Express js

Within my application, I have configured the static folder containing an 'index.html' file. I am looking to display this file on the route /profile Could this be done? ...

Position the Bootstrap 5 navbar at the top of the page and set it

Is there a way to use the fixed-top class on a navbar without making it full width and overlapping the container? This is the code: <div class="container" style="border: 1px solid;"> <nav class="navbar fixed-top n ...

Tips for maximizing bootstrap efficiency by eliminating unnecessary CSS code during rendering

Currently, we are developing an MVP using vue.js and have chosen to incorporate bootstrap for consistent styling of the elements on our single-page app. As we begin to implement the skin/theme into our application, we have encountered an issue with unnece ...

Is it feasible to maintain a button in a fixed position even after performing zoom in or zoom out on a mobile device?

I have been working on a website with a fixed position button that works well on desktop. However, I am wondering if it is possible to keep the button in a fixed position even after zooming in or out on a mobile device. Alternatively, can the button be r ...

Creating Multiple Promises in JavaScript: A Comprehensive Guide

How can I link multiple promises together? For example: var promise = new Promise(function(resolve, reject) { // Compose the pull url. var pullUrl = 'xxx'; // Use request library. request(pullUrl, function (error, response, bod ...

Struggling to align text to the far left within a text area using Bootstrap

When I accidentally place my cursor earlier in the text area, it starts writing from that point, leaving some unwanted spaces at the beginning. I prefer it to start from the extreme left, similar to how it behaves in input boxes. Below is the code snippet ...

Using surge.sh to deploy a Vue project

All day I've been attempting to deploy my Vue project on Surge.sh. The project is built using the webpack-simple template. Strangely, manual deployment from my CLI works fine. However, when I push it to GitHub and try deploying it from Travis CI, it ...

Interactive Click Actions on Rotated Divs

Does anyone know why, when clicking on the back side of a div that has been rotated with rotateY, the clicks pass through to the next div inside the body tag instead of registering on the intended div? And is there a way to fix this issue? You can view th ...

Running into issues while deploying a web application using the Express, Node, and MongoDB stack on Azure

Seeking guidance on deploying web apps to Azure. Currently facing issues with displaying EJS files after deployment. The app runs smoothly locally but fails to display anything on Azure, resulting in a 504 gateway timeout error. Here is the link to my GitH ...

Modifying the color of a div element solely through CSS styling

I am currently working with a set of buttons, available at this link. While browsing through this helpful post, I encountered an issue. Whenever I click on another element, the orange color of my button disappears. How can I maintain the orange color on t ...

Implementing user profile picture display in express.js: A step-by-step guide

I'm having trouble displaying the profile picture uploaded by users or employees in my application. Although the picture uploads successfully, it doesn't show up and gives an error when I try to redirect to the page. Cannot read property &ap ...