Webpack is failing to load the logo PNG image file

Is there a way to make the logo png file visible on the webpage?

I have been encountering issues with loading the image while other elements like HTML, css, and fonts are loading properly when the web pack is started.

List of Error Messages:

Refused to apply style from 'http://localhost:8080/forkify/dist/css/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
test.js:5 Imported module
index.js:14 Uncaught ReferenceError: element is not defined
    at eval (index.js:14)
    at Module../src/js/index.js (bundle.js:398)
    at __webpack_require__ (bundle.js:432)
    at bundle.js:526
    at bundle.js:529
logo.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)

Even after using file and uri loaders in webpack, I am still facing the same issue.

Content of index.js:

import num from './test';
import '../../dist/css/style.css';
import Logo from '../../dist/img/logo.png';

// Displaying the image on the div.
const myIcon = new Image();
myIcon.src = Logo;

element.appendChild(myIcon);

console.log(`I imported ${num} from another module test!`);

Webpack.config.js Configuration:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports={
    entry:'./src/js/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename:'js/bundle.js'
    
    },
    devServer: {
        contentBase:'./dist'
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html'
        })
    ],
    module: {
        rules: [
          {
            test: /\.css$/i,
            use: ["style-loader", "css-loader"],
          },
          {
            test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
            type: "asset/resource",
          },
        ],
    },

}; 

Package.json Details:

{
  "name": "forkify",
  "version": "1.0.0"
  "description": "Food app project",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "start:dev": "webpack serve --mode development --open"
  },
  "author": "BrewedLeo",
  "license"" "ISC",
  "devDependencies"": {
    "css-loader": "^5.0.1",
    "file-loader": "^6.2.0",
    "html-webpack-plugin"": "^5.0.0",
    "style-loader": "^2.0.0",
    "url-loader": "^4.1.1",
    "webpack": "^5.20.2",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^3.11.2"
  },
  "dependencies"": {}
}

https://i.sstatic.net/E61Jt.png

Answer №1

I believe you may have overlooked adding the outputPath property in your webpack.config.json file within the rules section. After specifying the test for file types like /.(png|...., make sure to include the loader as 'file-loader' and set the path as follows: options: { outputPath: 'images', } Personally, I find it more convenient to use copy-webpack-plugin for copying files into the dist folder.

plugins: [
        new CopyPlugin({
            patterns: [
                { from: "src/img", to: "img/" },
            ],
        }),
    ]

...and do not forget to add the missing element.

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

Load Angular component on demand with necessary dependencies

Searching for an elegant solution (without resorting to private APIs) to create a widget-style dashboard. The goal is to dynamically load components based on user role. Is there a way to import a component and its dependencies included in the component&ap ...

What is the best method for streaming files from Java to the browser while preventing the user from downloading and saving the file?

I'm new to this and feeling a bit lost. Here's the situation: I have a web app (built with JavaScript/Reactjs) and a backend (Java using Liferay API) The server contains files (File A, B, C, etc.) of various types: pdf, excel, audio, txt, etc. ...

Adding Material-UI icons dynamically in a React TypeScript project requires understanding the integration of imported icons

I have a collection of menu buttons with icons, stored in an array of objects. The icon names are saved as strings that correspond to Material UI icons: interface MenuButton { text: string, onClickFunction: Function icon: string } export defau ...

The Div element containing the rollover button/picture is failing to resize properly as the window is decreased in size

Whenever I try to resize my browser window in Firefox or Chrome, the div ends up moving instead of resizing. I've searched on stackoverflow for answers but haven't found any solutions yet. Can someone please help me with this issue? Here is what ...

Optimizing express server dependencies for a smooth production environment

After developing an application with a React front end and a Node server on the backend, I found myself wondering how to ensure that all dependencies for the server are properly installed when deploying it on a container or a virtual machine. Running npm ...

Parent div is positioned absolutely with overflow set to hidden

I am facing a unique requirement where the parent div has an absolute position and the child div also has an absolute position. Inside the parent div, I have set overflow: hidden to hide the extra width of the child div. However, this setup is not working ...

Django app with Vue.js integration is throwing a 405 error for Axios PUT request

I have a project in progress for managing inventory on a small scale. One of the functionalities I'm working on involves updating the stock quantity when a product is withdrawn. I've encountered an issue while using Axios and the .put() function, ...

Leveraging JavaScript to access the margin-top property of the HTML and body tags

I'm having trouble retrieving the "marginTop" style property from the <html> and <body> tags. While Chrome developer tools shows that margin-top is being set via in-HTML CSS: <style type="text/css" media="screen"> html { margin-top: ...

Create concise information in a single line with Twig

So, I basically need to display the following information inline on my website: Just like this: Family: Open Sans - Variant: Regular Italic Size: 15px - Line Height: 25px - Paragraph: 15px Color: grey_3 This is the code snippet: {% include "../compone ...

Having trouble accessing the iframe element in an Angular controller through a directive

My webpage contains an iframe with a frequently changing ng-src attribute. I need to execute a function in my controller each time the iframe's src changes, but only after the iframe is fully loaded. Additionally, I require the iframe DOM element to b ...

Find the name of the class using JavaScript

Is there a more elegant way to retrieve the class name of a class in JavaScript? I have implemented a method and would like to log any errors with a message indicating the class in which the error occurred. My current solution feels a bit "dirty": I am s ...

Determine the length of the string using an angular design

I have an input field and a span set up like this: <input type="password" name="account_password" placeholder="Enter your new password" autocomplete="off" ng-model="res.account.new_password" required="" ng-minlength="res.minlength" class="form-control" ...

JavaScript JSON YouTube API viewCount function is not functioning correctly

I'm struggling to retrieve the views of a video using JavaScript (Chrome Extension). Here is the code I have so far: $(function() { $.getJSON('http://gdata.youtube.com/feeds/api/videos/H542nLTTbu0?alt=json',function(data) { ...

Troubleshooting Angular 4's ng-selected functionality

I'm currently facing an issue with getting ng-selected to function properly. Initially, I attempted to simply add selected in the option tag, but later discovered that I should be using ng-select. Despite trying variations such as ng-selected="true" a ...

Creating a new row in a Tabulator component in React and retrieving the data

I have incorporated the react-tabulator library into my project and I am looking for guidance on how to dynamically add new rows once the tabulator has been rendered. Ideally, I would like to include a button below the tabulator that enables users to add a ...

Display the JSON information within a text input field using React JS

Below is the code I have written in the componentDidMount function: componentDidMount: function() { var url = document.URL; var currentId = url.substring(url.lastIndexOf('?') + 4); // Extracting the current ...

Methods for creating overlapping background images in breadcrumbs

I am having trouble with making the breadcrumb frame overlap the inside images. I attempted using z-index: -1, but it causes the images to disappear. Please refer to the attached image for a better understanding. Thank you. .breadcrumb { margin-botto ...

"encountered net::ERR_NAME_NOT_RESOLVED error when trying to upload image to s3 storage

I am currently developing an application using Angular. I have been attempting to upload a picture to my S3 bucket, but each time I try, I encounter this error in the console. https://i.stack.imgur.com/qn3AD.png Below is the code snippet from my upload.s ...

Switch to using addresses instead of latitude and longitude coordinates when utilizing the Google Maps API

I am looking to utilize Google Map Markers to indicate the locations where our services are offered. The code I have created using latitude and longitude is functioning properly, however, for my project I need to display city names instead of lat/long ...

Using Jquery to utilize next() function along with removeClass()

https://i.sstatic.net/6xlR6.png The image above demonstrates what I am trying to achieve - when a row is clicked, it should hide, the next row turns red and clicking on that hides it too, moving on to the next available row in the sequence. $(document).r ...