create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

react-scripts v4.0.0 causes local images to not load

Open douglasrcjames opened this issue 5 years ago • 33 comments

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

douglasrcjames avatar Nov 02 '20 21:11 douglasrcjames

In absolute terms, where are these images located? inside or outside the src folder?

PupoSDC avatar Nov 02 '20 21:11 PupoSDC

Inside src folder

douglasrcjames avatar Nov 02 '20 21:11 douglasrcjames

downgrade the version of react-scripts, thats works for me. I downgrade to 3.4.4 .

Acarye avatar Nov 02 '20 21:11 Acarye

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

Letcheverry avatar Nov 02 '20 21:11 Letcheverry

I have submitted a PR to fix this issue in #9934

n3tr avatar Nov 03 '20 02:11 n3tr

try const logov4 = require("../../assets/images/logo/logo-v4.png").default or import logov4 from "../../assets/images/logo/logo-v4.png"

qiqiboy avatar Nov 05 '20 02:11 qiqiboy

I don't know if this is the same issue but in 3.x this worked

+-public
|  +-images
|  |  +-abc.svg
|  +-index.html
+-src
  +-app.js
  +-app.css

Where app.js does import './app.css'; and app.css does foo { background url("/images/abc.svg"); }

Works in 3.x, fails in 4.0 with

./src/app.css
Error: Can't resolve '/images/abc.svg' in '/Users/gregg/temp/test/src'

greggman avatar Nov 05 '20 05:11 greggman

@greggman how/have you fixed this?

MobeenAshraf avatar Nov 05 '20 08:11 MobeenAshraf

For now I just reverted to 3.x I haven't had time to check if @n3tr 's patch is related or not

greggman avatar Nov 05 '20 09:11 greggman

I have to use require('./to/img/a.png')?.default 😢

littlee avatar Nov 06 '20 06:11 littlee

I can confirm this issue. I tried to upgrade CRA 4.0.0 and images are not loaded.

trainto avatar Nov 06 '20 16:11 trainto

+1 images are not loading after upgrading to react-scripts 4.0.0

arjunkomath avatar Nov 06 '20 23:11 arjunkomath

same here, updated to react-scripts v4.0.0 and images are not loading anymore all my img sources are in the src folder and I call them via src={require("./img/example.svg")} React scripts v3.4.4 works fine

Arnaudpagn avatar Nov 12 '20 10:11 Arnaudpagn

Issue still there

Deivi2 avatar Dec 14 '20 14:12 Deivi2

Issue is still here -- thank you.

brienpafford avatar Jan 26 '21 19:01 brienpafford

Some of us have 3rd party things in our public folder that are not part of the build. We used to be able to reference those things. Now we can't

greggman avatar Feb 10 '21 00:02 greggman

Just use normal import import logo from '../../assets/images/logo/logo-v4.png' it should work. For some reasons require is importing images as modules.

vishnusajev avatar Feb 24 '21 10:02 vishnusajev

How is this still an issue? Was this intended? Do I need to update all my projects using require() to load images? I am now getting a high severity vulnerability with immer package that depends on react-scripts and it wants me to update react-scripts to at least 4.0.3.

douglasrcjames avatar Feb 25 '21 19:02 douglasrcjames

I have this issue too I spend a day on it, this should be fixed......

lewatt23 avatar Feb 28 '21 08:02 lewatt23

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

suhailalopa avatar Mar 01 '21 12:03 suhailalopa

try const logov4 = require("../../assets/images/logo/logo-v4.png").default or import logov4 from "../../assets/images/logo/logo-v4.png"

this worked for me with react-scripts 4.0.3

I was also seeing that immer version warning when i rolled back to 3.4.4

chuckponzi avatar Mar 07 '21 04:03 chuckponzi

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 :facepalm:

Adding the ?.default works for me too on 4.0.3.

alyssaruth avatar Apr 16 '21 14:04 alyssaruth

Bump. Not fixed. Currently using require('../images/img-9.jpg').default to display images as a work around.

stryker2k2 avatar May 22 '21 00:05 stryker2k2

Try this it will definitely work src={process.env.PUBLIC_URL + your image URL}

Saurav-Nepal avatar May 30 '21 14:05 Saurav-Nepal

Bump. Not fixed. Currently using require('../images/img-9.jpg').default to display images as a work around.

Try this it will definitely work src={process.env.PUBLIC_URL + your image URL}

Saurav-Nepal avatar May 30 '21 14:05 Saurav-Nepal

Bump. Not fixed. Currently using require('../images/img-9.jpg').default to display images as a work around.

Try this it will definitely work src={process.env.PUBLIC_URL + your image URL}

Nice work for me! Thanks.

HipSiGa avatar Jun 10 '21 06:06 HipSiGa

Importing file types that are supported (png, svg, etc), works. But in 3.x I would use requires to get urls of non-supported files like .csv. Changing my requires to use .default at the end is the solution I'm going with for now, but I'd prefer to use es6 imports import someFileUrl from '../../data.csv', so I don't have to disable the @typescript-eslint/no-require-imports lint rule.

Using process.env all over my repo, in render methods, is rather gross. As is using global variables like process unless absolutely necessary.

k-funk avatar Jul 02 '21 21:07 k-funk

I have to use require('./to/img/a.png')?.default 😢

Thanks, i used this way and it's running ok

nnmquang avatar Nov 16 '21 16:11 nnmquang

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

stale[bot] avatar Jan 09 '22 04:01 stale[bot]

This issue is still present in v5.0.0

kens-visuals avatar Feb 02 '22 23:02 kens-visuals