Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Require statement doesn't work as expected?

I want to show some images depending on how many I get through an array, so I can't really use an import statement.

I googled and I had found that I could just use require(), but it doesn't work as expected.

GameImage.tsx

import './css/GameImage.css';

type Image = {
    imgSrc: string,
    imgAlt: string
}

type Images = {
    images: Array<Image>
}

function GameImage(props: Images) {
    return (
    <div className="game-details-image-wrapper">
        {props.images.map((pair) => (
            <img className="game-details-image" src={require(pair.imgSrc).default} alt={pair.imgAlt} />
        ))}  
    </div>);
}

export default GameImage;

how I render it:

<GameImage images={[{imgSrc: './img/Untitled.png', imgAlt: 'test'}]}/>

This way, I will get

Cannot find module './img/Untitled.png'

but if I modify the img tag like this:

<img className="game-details-image" src={require("./img/Untitled.png").default} alt={pair.imgAlt} />

it works properly.

Why does it happen and how can I fix it?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!