Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use <img src> in webpack?

Tags:

webpack

When I'm trying to load an image

<img class="brand" alt="Brand" width="50%" height="50%" src="/logo.jpg">

It produces the following error:

logo.jpg:1 GET http://localhost:8080/logo.jpg 404 (Not Found)

webpack.config.js:

module: {   
    loaders: [
        {
            test: /\.html$/,
            exclude: /index\.html$/,
            loader: 'html-loader?root=./assets/images&interpolate&name=./views/[name].[ext]'
        },
        {
            test: /\.(png|jpg|jpeg|gif)$/,
            loader: 'url-loader?limit=10000&name=./assets/images/[name].[ext]'
        }
    ]
}
like image 852
Akansha Srivastava Avatar asked Oct 26 '17 13:10

Akansha Srivastava


People also ask

Where do you put images in Webpack?

First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/. }; It's quite similar to setting up fonts with Webpack.

How do I add an image to a react in Webpack?

Referencing to images Webpack can pick up images from style sheets through @import and url() assuming css-loader has been configured. You can also refer to your images within the code. In this case, you have to import the files explicitly: import src from "./avatar.

Does Webpack bundle images?

Using Webpack Similar to CSS, we can use Webpack for image bundling. First, we need to install two loaders as we did with CSS bundling. Here the loaders we use are the file loader and the HTML loader.


1 Answers

You have to import/require your image in your entry js file and that image will be processed and added to your output directory and the Logo variable will contain the url of that image after processing

import Logo from './logo.jpg';

Another way is to use html-loader and import it in your entry js file . Then you can use the usual src attribute as in html.

like image 144
Anoop D Avatar answered Oct 14 '22 10:10

Anoop D