0

I just updated to create-react-app v4

So my updated dependencies looks as follows

"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",

I have deleted all my node_modules folder and reinstalled them again

I have included all my images in an images folder which is present in src folder of create-react-app. When i go back to react-scripts 3.4.4, my images are rendering properly but in v4.0.0 images are not rendered at all. I am using both png and svg in my app and both fail to render.

Edit: I have found that images in public folder seems to render without any issue. But i want the images used by my components in src folder which are still not rendering and placing images used by components in src folder is a good practice. Do I store Image assets in public or src in reactJS?

BraveEvidence
  • 53
  • 11
  • 45
  • 119

3 Answers3

4

Try updating require to import statement.

const myImg = require('../imagepath.jpg')

to

import myImg from '../imagepath.jpg'
Shyam
  • 1,364
  • 7
  • 13
0

Are you using NODE_PATH?

There is a breaking change in v4.0.0.

Removed typescript flag and NODE_PATH support

We've removed the deprecated typescript flag when creating a new app. Use --template typescript instead. We've also dropped deprecated NODE_PATH flag as this has been replaced by setting the base path in jsconfig.json.

If you are using it, use jsconfig.json instead.

yudhiesh
  • 6,383
  • 3
  • 16
  • 49
augustine
  • 538
  • 4
  • 15
0

Shyam's answer works when your image URL is static, however, if you want the URL to be created dynamically it won't work.

If that's the case you can use this answer from GitHub:

require("../../assets/images/others/"+loginBgImage)

loginBgImage can have different values (login-image.jpg, login-new-image.jpg)

this got fixed using require("../../assets/images/others/"+loginBgImage).default

The .default after the require does the trick.

Diego Ruiz
  • 300
  • 1
  • 4
  • 7