Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't resolve '/images/img-2.jpg' in 'E:\React\react-demo\src'

Here is my code i am trying to add image but its shows error

background-image: url('/images/img-2.jpg');

Failed to compile.

./src/App.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/App.css)
Error: Can't resolve '/images/img-2.jpg' in 'E:\React\react-demo\src'
like image 909
Avinash gupta Avatar asked Dec 03 '20 12:12

Avinash gupta


People also ask

How do I display a JPEG image in React?

To display an image from a local path in React:Download the image and move it into your src directory. Import the image into your file, e.g. import MyImage from './thumbnail. webp' . Set the src prop of the image element to the imported image, e.g. <img src={MyImage} alt="horse" /> .


3 Answers

Because you asked for an image not founded in the src folder! maybe you're trying to access the images in the public folder at your React project.

My Friend's life is easy, React - using Webpack - converts the jpg/png..etc images to base64 then replaces the URL to the base64... like this

enter image description here

So, you need to make an assets/images folder 📁 inside the src folder that contains all your images, then import the link in your CSS

  background-image: url("./assets/images/logo512.png");

Again, React will convert the image to bases64! That will make the image not depend on the folder Path.

like image 107
Tawfeeq Amro Avatar answered Nov 08 '22 14:11

Tawfeeq Amro


I got myself in a similar situation. Here's the fix I made: The error is saying that you do not have an image 📁 under src 📁

simply move the image 📁 under src then redirect the URL to it

background: url('/src/images/img-2.jpg');
like image 29
acmatias Avatar answered Nov 08 '22 14:11

acmatias


Module not found: Error: Can't resolve '/images/img-2.jpg'

Add your images to your src ... pretty much move it from public to the src folder

background-image: url('/src/images/img-2.jpg');
like image 3
Babatunde Olaniyi Avatar answered Nov 08 '22 12:11

Babatunde Olaniyi