'IMAGE: You may need an appropriate loader to handle this file type
I can't figure what is the proper loader to load images in ReactJS webpack,
May you give me a hand? I get this error:
Module parse failed: /Users/imac/Desktop/fakeeh/imgs/logo.png Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
Here is webpack config:
const path = require('path');
module.exports = {
// the entry file for the bundle
entry: path.join(__dirname, '/client/src/app.jsx'),
// the bundle file we will get in the result
output: {
path: path.join(__dirname, '/client/dist/js'),
filename: 'app.js',
},
module: {
// apply loaders to files that meet given conditions
loaders: [{
test: /\.jsx?$/,
include: path.join(__dirname, '/client/src'),
loader: 'babel-loader',
query: {
presets: ["react", "es2015", "stage-1"]
}
}],
},
// start Webpack in a watch mode, so Webpack will rebuild the bundle on changes
watch: true
};
Much appreciated!!
Solution 1:[1]
I also encountered this problem too and I've found a workaround.
First, you need to install the two loaders(file-loader, url-loader). e.g. $ npm install --save file-loader url-loader
If you want to support the css. Make sure you install the style loaders. e.g., $ npm install --save style-loader css-loader
Next, you update the webpack config, kindly check below my sample configurations. Hope it helps.
module: {
loaders: [{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
}, {
test: /\.css$/,
loader: "style-loader!css-loader"
}, {
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'url-loader?limit=100000' }]
},
Solution 2:[2]
You can use file-loader. You need to install it first using npm and then edit your webpack config like this
module: {
// apply loaders to files that meet given conditions
loaders: [{
test: /\.jsx?$/,
include: path.join(__dirname, '/client/src'),
loader: 'babel-loader',
query: {
presets: ["react", "es2015", "stage-1"]
}
},
{
test: /\.(gif|svg|jpg|png)$/,
loader: "file-loader",
}],
},
Solution 3:[3]
With Webpack 3, you can use url-loader
as follow:
Install url-loader
:
npm install --save url-loader
Then, in your Webpack config:
module.exports = {
module: {
rules: [
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192
}
}
]
}
]
}
}
Finally, in your code:
<img src={require('./path/to/image.png')} />
Solution 4:[4]
these two steps worked for me
Asset Resource Loader ("webpack": "^5.70.0")
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.jsx',
output: {
path: path.join(__dirname, '/dist'),
filename: 'bundle.js',
assetModuleFilename: '[name][ext]', // (2)
},
resolve: {
extensions: ['.jsx', '.js'],
},
module: {
rules: [
{
test: /\.jsx?$/,
use: ['babel-loader'],
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|jpe?g|gif|svg)$/i, // (1)
type: 'asset/resource',
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
}),
],
};
Solution 5:[5]
Sometimes the solution is simpler and right in front of you.
I immediatly started to google for this issue. But I was missing jpg extention in the test.
So.. my test is:
test: /.(png|woff(2)?|eot|ttf|svg|gif|jpe?g)(\?[a-z0-9=\.]+)?$/
Solution 6:[6]
If include property is defined, make sure it contains the path of the folder which contains the image. For me, that was the issue.
{
test: /\.(png|jpg|ttf|eot|svg|woff(2)?)(\S+)?$/,
include: [/react-notifications/, /goodtables-ui/, /@mdi\/font/, /clear-ui/],
use: ['file-loader']
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Prakash Sharma |
Solution 3 | philippe_b |
Solution 4 | kirill-petrov |
Solution 5 | ruinobreferreira |
Solution 6 | Sushil Kumar |