Skip to content

Bug: Cannot use require() to display image on my machine. #10088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mmacu opened this issue Nov 17, 2020 · 7 comments
Open

Bug: Cannot use require() to display image on my machine. #10088

mmacu opened this issue Nov 17, 2020 · 7 comments

Comments

@mmacu
Copy link

mmacu commented Nov 17, 2020

Describe the bug

Cannot use require() to display image on my machine. During preparation of this bug report I've created the same base code in Codesandbox and it works there.

Did you try recovering your dependencies?

npm --version
6.14.8

Which terms did you search for in User Guide?

require

Environment

Environment Info:

  current version of create-react-app: 4.0.0
  running from C:\Users\Michael\AppData\Roaming\npm-cache\_npx\13776\node_modules\create-react-app

  System:
    OS: Windows 10 10.0.17763
    CPU: (4) x64 Intel(R) Core(TM) i5-4690K CPU @ 3.50GHz
  Binaries:
    Node: 12.19.0 - C:\Program Files\nodejs\node.EXE
    Yarn: Not Found
    npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 86.0.4240.198
    Edge: Spartan (44.17763.831.0)
    Internet Explorer: 11.0.17763.771
  npmPackages:
    react: ^17.0.1 => 17.0.1
    react-dom: ^17.0.1 => 17.0.1
    react-scripts: 4.0.0 => 4.0.0
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Create app with npx create-react-app .
  2. Try to get image element with <img src={require("path/to/image/image.png")} /> in App.js render return.

Expected behavior

browser

Should display icon two times. Once using import variable, once using required. Works like that on Codesandbox website. Refer to demo below.

Actual behavior

This is how it looks while I'm running minimal example locally in project created with npx create-react-app .

local

Reproducible demo

https://codesandbox.io/s/react-require-bug-1gh2i?file=/src/App.js:67-292

@glyph-cat
Copy link

So far no problems seen after running it both on sandbox and locally.

But I do have a similar encounter in another project that also uses require() for an image. It was fixed by doing require("path/to/image/image.png").default, although I'm not sure why. Perhaps you can try that and see if it helps?

That project was using React Scripts 3.4.3 and React 16.13.1. The bug was observed after I updated them to 4.0.0 and 17.0.1 respectively. But I'm not sure if it was really caused by the version update.

@mmacu
Copy link
Author

mmacu commented Nov 23, 2020

Yes. Adding .default seems to get it to work, so at least there is a workaround for this bug. Thanks.

@kyler-hyuna
Copy link

Experiencing this as well. Only after react 17.

@codepiyush
Copy link

this issue is still there and it is annoying. Just did npx create-react-app and tested image with require it loads [object Module] instead of the image path. import image still works though. adding .default fixes the issue.

@RayPS
Copy link

RayPS commented Jan 24, 2021

Same issue for me, latest CRA with React ^16.14.0.
Images loads [object Module]

@RayPS
Copy link

RayPS commented Jan 24, 2021

Duplicate #9992

@stale
Copy link

stale bot commented Jan 9, 2022

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 stale bot added the stale label Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants