react-visibility-sensor: Module not found: Error: Can't resolve 'React' in '/home/capaj/project/node_modules/react-visibility-sensor/dist'
I have webpack 4 and react-visibility-sensor 5.0.2
and when I import like this:
import VisibilitySensor from "react-visibility-sensor"
I get
ERROR in ../node_modules/react-visibility-sensor/dist/visibility-sensor.js
Module not found: Error: Can't resolve 'React' in '/home/capaj/git_projects/looop/project-alpha/node_modules/react-visibility-sensor/dist'
@ ../node_modules/react-visibility-sensor/dist/visibility-sensor.js 4:286-324
@ ./src/pages/student-topic-detail-page/StudentTopicDetailPage.tsx
@ ./src/components/LooopRoutes.tsx
@ ./src/components/App.js
@ ./src/app/app.js
@ ./src/index/index.js
@ multi webpack-dev-server/client?http://localhost:4001 webpack/hot/dev-server ./src/index/index
ERROR in ../node_modules/react-visibility-sensor/dist/visibility-sensor.js
Module not found: Error: Can't resolve 'ReactDOM' in '/home/capaj/git_projects/looop/project-alpha/node_modules/react-visibility-sensor/dist'
@ ../node_modules/react-visibility-sensor/dist/visibility-sensor.js 4:286-324
@ ./src/pages/student-topic-detail-page/StudentTopicDetailPage.tsx
@ ./src/components/LooopRoutes.tsx
@ ./src/components/App.js
@ ./src/app/app.js
@ ./src/index/index.js
@ multi webpack-dev-server/client?http://localhost:4001 webpack/hot/dev-server ./src/index/index
when I change to import the source file:
import VisibilitySensor from 'react-visibility-sensor/visibility-sensor'
it works great.
The webpack thingy seems to think I the app run in an UMD loader and hence it tries to load deps from globals.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 5
- Comments: 15
Actually on second thought, it is better to configure webpack to skip transpilation for
react-visibility-sensor
instead of importing what is essentially a private file. If this package changed its file structure that could cause your code to crash. So instead configurebabel-loader
as follows and leave your imports and jest config alone!import VisibilitySensor from 'react-visibility-sensor/visibility-sensor'
saving my life!
+1, getting a similar error with
ReactDOM
:A similiar issue with react Dom not resolved ? Any activity here at all ?
Until this get’s resolved I published https://github.com/joshwnj/react-visibility-sensor/pull/151 bundling fix under https://www.npmjs.com/package/@k.sh/react-visibility-sensor if anyone needs a drop-in-fix.
This issue seems to stem from the webpack configuration. I’m not sure why it is written as
React
andReactDOM
instead ofreact
andreact-dom
. This can be seen on lines 12 and 18 ofwebpack.config.js
.This causes a webpack output in /dist/visibility-sensor.js of:
Of course these modules don’t exist because they are named incorrectly. Perhaps I’m missing something, but it seems if this were changed in the webpack configuration, this would be resolved. If someone can confirm this, I’m happy to submit a PR.
Working Fix (Works for me at least…) For me the issue only occurs when running locally with Storybook. As such, I simply modified the bundled instance. If it’s breaking in production though, you need to fork and modify the webpack configuration. For working locally only, in /dist/visibility-sensor.js, you can just modify line 5 as follows: