14

I've used <img src={require('./somRelativePath/image.jpg)} in React many times. However, this time it seems not to be working. There are no errors whatsoever (such as that the image was not found etc.) but the broken image on website.

After inspecting the element I was somewhat confused by the transpiled result in browser:

<img src="[object Module]" style="width: 5rem;">

It appears as if it loads the image as a component not the acutual file. I've created the app with npx create-react-app and haven't ejected it so far. So there is no error in babel or webpack configuration as it is currently handled by react under the hood.

Importing it with import statement works just fine:

import calendarPic from '../assets/pictures/calendar.svg';

Unfortunately that's not the solution because I have the local images saved in json and it would be definitely quite repetitive and ineffective as well to load all 50 images.

With the same npx create-react-app I've made a handful of mini-projects before but have never come across such a perplexing, yet so basic error. I'd be so thankful for any response as I've skimmed every possible solution throughout the internet.

Thank you again and have a lovely day!

2
  • 1
    Does this answer your question? link Commented Nov 19, 2020 at 11:39
  • 1
    Well I do not have access to the .webpack configuration file as it's handled by react, so I'd have to eject it. Do you recommend that @Darkshadow ? Commented Nov 19, 2020 at 11:42

3 Answers 3

34

use this one, it's work for me

<img src={require('./somRelativePath/image.jpg).default}


explanation :

value from let image1require('./somRelativePath/image.jpg) is different with import calendarPic from ./somRelativePath/image.jpg';`

if you console them, value from calendarPic is a path, but if you use require the value is an object like like here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, I've struggled with this issue for a while now, I always ended up using alternative methods that I felt didn't work too well for me.
add .default and it's working..thx man
7

I guess the problem is the location of the image. When you use create-react-app the app will be bundle into the public folder. Then the require statement would start to fetch the image - in this case in relative to the public folder and not to the src folder.

What I suggest you to do is try to move the image into the public folder and try using the src with URL relative to the public folder. Demo here

2 Comments

Yeah, that is bound to work. Do you use it this way every time you code React app?
I usually use the import Image from './source/ because I don't use a lot of image. Read more about the tradeoff between those two methods here
1

None of the answers above worked for me. Instead I used require(path) outside the img tag and assigned it into a variable, then used the variable inside the img tag src. It works that way.

const F = {
  name: "Weiß",
  image: require('./images/0453-Eastbourne-F961-F01.png')
}

Inside the image tag:

src = { F.image }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.