Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

shu Chan
shu Chan
2,951 Points

React.js I can't load local images

Hi! I'm having issues loading local images

import React, { PureComponent } from "react";

// Components
import SplashPage from "../components/SplashPage.jsx";
import Button from "../components/Button.jsx";

class Home extends PureComponent {
  render() {
    return (
      <SplashPage>
        <div className="home-page">
          <img src={"./Logo.png"} />
          <br />
          <h2 className="title">Welcome to our humble Shop</h2>
          <br />
          <Button
            onClick={() => {
              this.props.history.push("/shop");
            }}
          >
            Go shopping
          </Button>
        </div>
      </SplashPage>
    );
  }
}

export default Home;

The png file in question is in the same directory, but nothing shows up!

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

There can be multiple reasons why this isn't working.

Are you using webpack?

4 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

If you are using webpack, then I believe you need to use the require() importing method to use local files, as they will be bundled with webpack's file-loader.

Example:

<img src={require("./Logo.png")} />

That should work. If not let me know, it could be either a pathing issue or a webpack/server setup issue.

shu Chan
shu Chan
2,951 Points

Yes I am using webpack

shu Chan
shu Chan
2,951 Points

Hey, unfortunately its still not working I get this message "Uncaught Error: Cannot find module './Logo.png'"

I just came across this question when googling for the same problem. I solved it by making sure my images are in the public folder. Hope this helps anyone with the same issue.