Webpack cant build the project, because cant resolve image.
Project structure
JavaScript
x
9
1
project/
2
├─ node_modules/
3
├─ src/
4
│ ├─ assets/
5
│ │ ├─ images/
6
│ │ ├─ styles/
7
├─ package.json
8
├─ webpack.config.js
9
Here is webpack.config.js
JavaScript
1
9
1
{
2
test: /.(JPG|gif|svg|gif|png)(?v=[0-9].[0-9].[0-9])?$/,
3
type: 'asset/resource',
4
generator: {
5
filename: 'assets/images/[name].[contenthash:7][ext]'
6
},
7
use: [{loader: 'url-loader'}]
8
},
9
and main.css
JavaScript
1
5
1
.b-promo {
2
background: url(../assets/images/bg.png) top no-repeat;
3
background-size: cover
4
}
5
Error message
JavaScript
1
5
1
ERROR in ./assets/css/main.css
2
Module build failed (from ../node_modules/mini-css-extract-plugin/dist/loader.js):
3
ModuleBuildError: Module build failed (from ../node_modules/css-loader/dist/cjs.js):
4
Error: Can't resolve '../assets/images/bg.png' in '/home/path/to/project/src/assets/css'
5
Advertisement
Answer
Seems like url() should take a string as a parameter see here
So try in main.css (and make sure that the relative path to bg.png file from main.css one is correct):
JavaScript
1
4
1
.b-promo {
2
background: url("../assets/images/bg.png") top no-repeat;
3
background-size: cover;
4
}