1

Actually trying to check if file(image) exists to add it to render.

Confused that this code works

let i = 2;
let s = require('../../public' + this.cardData.image.optional_path + '-' + i + '.jpg');

and as a result s = Module

And this one produce an error

let i = 2;
let path = '../../public' + this.cardData.image.optional_path + '-' + i + '.jpg';
let z = require(path);

Error message:

Uncaught Error: Cannot find module '../../public/images/catgory1/images-2.jpg'

Did i missed something ?

Subdigger
  • 2,166
  • 3
  • 20
  • 42
  • Might it be releated to webpack, or something that fails to correctly parse require patch? – majkrzak Jan 22 '21 at 07:43
  • @majkrzak not sure what are you talking about – Subdigger Jan 22 '21 at 07:50
  • https://stackoverflow.com/questions/52171142/importing-image-dynamically-react-js-require-img-path-cannot-find-module could help? – Giovanni Esposito Jan 22 '21 at 07:52
  • @GiovanniEsposito nope. i need to build a list of images (for corousel) that exists. linked article displays image without checking. – Subdigger Jan 22 '21 at 08:01
  • @Subdigger I already had a problem like that and it was related to the dynamic part of the image link. Have you tried to define a static list of available images like https://stackoverflow.com/a/63430097/13897065 ? – Giovanni Esposito Jan 22 '21 at 08:47
  • @GiovanniEsposito as you can see in question - tere 2 way of doing the same. upper one is work fine. even if i declara it full text path it works fine. but i can not declare all imeges. names come from apy request. – Subdigger Jan 22 '21 at 10:14

0 Answers0