n1ksON2022-04-21 22:12:38
n1ksON, 2022-04-21 22:12:38

How to configure webpack to display images from css?

Images that I import into components are displayed correctly. But the pictures written in the css files are not displayed.
I rummaged through everything in search of a normal config so that everything worked, but the problem remains. And the old configs that I used before do not work.

const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const config = {
  entry: [
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  plugins: [
    new MiniCssExtractPlugin()
  module: {
    rules: [
        test: /\.(js|jsx)$/,
        use: 'babel-loader',
        exclude: /node_modules/
        test: /\.css$/,
        use: [
        test: /\.(png|jpg|jpeg|svg|gif)$/,
        include: [
          path.resolve(__dirname, './dist/img/') 
        use: [{
          loader: 'file-loader',
          options: {
            name: './dist/img/[hash].[ext]',
        test: /\.(ttf|eot|woff|woff2|png|jpg|jpeg|svg|gif)$/,
        loader: 'url-loader'
  devServer: {
    'static': {
      directory: './dist'

module.exports = config;

Tell me what else needs to be written so that the images specified through the background-image are shown. I even tried to write style={{background: url(imported image here)}} in the component near the tag - it still doesn't work

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question