[CRA4] require doesn't work with images
See original GitHub issueCRA 4 looks really neat, but I’ve had this issue for the past week:
Describe the bug
require
results in [Object module]
instead of the image URL in CRA 4 but not CRA 3
see https://github.com/facebook/create-react-app/discussions/9720
Did you try recovering your dependencies?
yes
yarn --version
: 2.2.2
Which terms did you search for in User Guide?
image not loading, require image typescript, typescript load image
Environment
Environment Info:
current version of create-react-app: 3.4.1
running from /Users/suman/.config/yarn/global/node_modules/create-react-app
System:
OS: macOS 11.0
CPU: (4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
Binaries:
Node: 12.14.1 - /usr/local/bin/node
Yarn: 2.2.2 - /usr/local/bin/yarn
npm: 6.14.8 - /usr/local/bin/npm
Browsers:
Chrome: 85.0.4183.121
Firefox: 81.0
Safari: 14.0.1
npmPackages:
react: Not Found
react-dom: Not Found
react-scripts: Not Found
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
- create a CRA TypeScript with CRA v3
- edit the CRA version (
react-scripts
inpackage.json
) to^4.0.0-next.98
. yarn install
yarn start
Expected behavior
images load in CRA 4 (the same way they did in CRA 3)
Actual behavior
with CRA 4:
with CRA 3:
both are running on the exact same project source code
Reproducible demo
https://github.com/sumanthratna/cra-4-ts-require
if you clone, change the react-scripts version to 3, and then rerun, the React logo loads perfectly. If you make no changes to the git repo, you’ll see that the logo doesn’t load.
P.S. thanks for CRA!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:10
Top Results From Across the Web
Requires not loading images in CRA
The problem is in the second element of the array, the image doesn't load with the 'require' but does so with the import...
Read more >Different Ways to Display Images in React Apps
In this article, I will explain different ways of including images in React applications. In general practice, we would include the images in...
Read more >how to add image in react from public folder
You don't need any webpack configuration for this. In your component just give image path. React will know its in public directory.
Read more >Images
Adding and changing images doesn't require app recompilation, ... In order for this to work, the image name in require has to be...
Read more >Props.img not displaying - JavaScript
images /${props.img}`} className="katie1"/> <div ... I've tried moving the images folder to the public folder but react doesn't seem to like ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
See https://stackoverflow.com/a/65151059
I ended up having to update a bunch of files to add
.default
afterrequire()
. I found them like this:I’m seeing the same thing locally after upgrading to 4.0.0