0

i'm trying to load some images whose path is stored inside a JSON file, in reactjs. My JSON file contains an array of objects in which there is the path of my images. Inside my component, i map my JSON data and can access every properties. Nevertheless, when i try to load the image, it keeps showing me the same error : Uncaught Error: Cannot find module '.png' when loading from a json file. Where i'm trying to load the image loading image

My JSON file JSON file

I've added the import of my json file at the beginning of my file

import projects from "../data/importantProject.json"

The error error

Has anyone experienced the same issue ?

I tried to add a based url, and a "" as I saw on the web but it keeps showing me the same error.

1
  • You should add your code as text rather than screenshots! Commented Nov 2, 2022 at 16:34

2 Answers 2

1

1 ----> In json file instead of full file path just write the file name ie : fap_logo.png

2 ----> In react component write <img src={require(`../assets/images/`+project.logo)} alt="image" />

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

Comments

0

You just need to set your image src as:

const imgsrc = require("../assets/images/"  +project.logo).default;

Note at 'default' at the end of the require method:

require(filepath).default

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.