BradleyB19
BradleyB19

Reputation: 177

React "Module Not Found" when loading a local image

I am trying to load an image using React using the following code:

const DogImage = require("../../public/dog.jpg");
console.log(DogImage);
...

<img src={DogImage} width="100px"/>

but I am getting the error:

enter image description here

the console log statement gives:

t

Please let me know if you have any suggestions or if I can provide more info!

EDIT: The file listed in the console.log (dist/8ce0...) exists when built

Upvotes: 0

Views: 159

Answers (1)

Slava Knyazev
Slava Knyazev

Reputation: 6081

The path is going into the default property, which is a special property used by the import syntax. Try:

import DogImage from '../../public/dog.jpg';

OR

const DogImage = require('../../public/dog.jpg').default;

Anything else would require digging into the webpack config.

Upvotes: 1

Related Questions