DEV Community

Michael Fazekas
Michael Fazekas

Posted on

2

Why React can not access images from public

How I got to this question.

So the other night I was working on a small react project. I’ve imported images once before with no issues. But this one was giving me a module error with webpack.

Here’s what I found.

So when you save images or images folders it best to use src/assets Inside your react project, don’t save them to the public/folder even though you can tweak that html file webpack does not process it because that folder is technically not part of the final bundle when you build production. The fastest way is to move images to src file where all your ‘component/folder’ files are in.

Okay but why?

Images stores in Your src/folder directory is apart of the final bundle with webpack, so they have a bonus of being able to be imported as a JavaScript module.

So in addition if it’s something you’re going to be importing to put it In your src/.

Top comments (1)

Collapse
 
mayankdv profile image
Mayank Arora

Thank you. This helped me to solve the issue of image not loading in my react project.

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay