create-react-app: react-scripts v4.0.0 causes local images to not load
Describe the bug
Upgrading from v3.4.4 to v4.0.0 (using npm install react-scripts@latest
) causes my local images (using src={require("../../assets/images/logo/logo-v4.png")}
) to not load, however external images load. (see StackOverflow post here ) Fixed by simply downgrading to v3.4.4.
Environment
Tested using MacOS Catalina v10.15.7 and Windows 10 on Chrome Version 86.0.4240.111
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 32 (2 by maintainers)
Commits related to this issue
- Removed typescript and restructured loading in images https://github.com/facebook/create-react-app/issues/9992 — committed to douglasrcjames/dougs-react-boiler by douglasrcjames 3 years ago
- Workaround facebook/create-react-app#9992 introduced by react-scripts upgrade — committed to dbjorge/poe-betrayal by dbjorge 3 years ago
try
const logov4 = require("../../assets/images/logo/logo-v4.png").default
orimport logov4 from "../../assets/images/logo/logo-v4.png"
Issue still there
I have to use
require('./to/img/a.png')?.default
😢downgrade the version of react-scripts, thats works for me. I downgrade to 3.4.4 .
Issue is still here – thank you.
I don’t know if this is the same issue but in 3.x this worked
Where app.js does
import './app.css';
andapp.css
doesfoo { background url("/images/abc.svg"); }
Works in 3.x, fails in 4.0 with
I recomend to downgrade react-scripts to 3.4.4 that bug does not happened anymore with that version, i hope that they fix that bug in the next patch
how do we solve the problem for loading images dynamically, say my image name depends on a variable
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
I have submitted a PR to fix this issue in #9934
Ugh, I’ve been hit by this too. Spent longer than I care to admit trying to figure out what the problem was. I had a broken serviceWorker to deal with as well - and mistakenly thought both were the same problem 🤦
Adding the
?.default
works for me too on 4.0.3.In absolute terms, where are these images located? inside or outside the
src
folder?I stumbled over this issue because after the upgrade from v4.0.3 to v5.0.0 some of my imports broke.
I discovered the following with image require calls in v5.0.0:
.default
anymore. With.default
the variable is undefined..default
. Without it the variable looks like this:Importing it normally worked for both cases. I didn’t test any other file extensions.
I’m wondering if there’s any documentation for this.
EDIT: Found the definition: react-app.d.ts
This issue is still present in v5.0.0
Thanks, i used this way and it’s running ok