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?

3 Answers 3

4

Try updating require to import statement.

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

to

import myImg from '../imagepath.jpg'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Is require deprecated in create-react-app v4?
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.

1 Comment

I am not using NODE_PATH and not using Typescript. I am using JS
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.

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.