0

I want to upload images from files in my React application (I use create-react-app).It didn't show up and the error message appeared in the terminal.

I tried the proposed solution in the following questions but It didn't work out for my app.

React won't load local images

Dynamically Add Images React Webpack

The error message is

Module not found: Can't resolve './Image.png' in '/Users/ayumi/my-app/src/components'

Main.js

import React from 'react';
import Image from './Image.png';


export default class Parent extends React.Component {
  render() {
    return (
      <div>
        <img src={Image} alt=""} />
      </div>
    );
  }
 }

File structure

|-- src
|   ` app.js
|    ` image.jpg
|   `-- components
|      `Main.js

Edited Thanks to Chris's comment, it figure it out that just one comma was missing in import area.

import React from 'react';
import Image from '../Image.png'; 
2
  • if ./ resolves to /Users/ayumi/my-app/src/components and your image is one level higher, just import it like../Image.png. So just add another .. Commented Sep 14, 2017 at 13:40
  • Oh my god. It's a silly mistake. It works fine now. Thank you so much! Commented Sep 14, 2017 at 13:50

1 Answer 1

1

You need to go up a directory to import your png like so:

import Image from '../Image.png';
Sign up to request clarification or add additional context in comments.

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.