Does not work in react-native
RossmacD opened this issue · comments
When using this library the frontend will throw an error when using react native. This is because of the way that the request file checks that the typeof window is not undefined
https://github.com/enmasseio/timesync/blob/master/src/request/request.js
The package at "node_modules/timesync/lib/request/request.node.js" attempted to import the Node standard library module "http".
It failed because the native React runtime does not include the Node standard library.
Failed building JavaScript bundle.
There is a fork of this project for react native but its outdated and not on npm, so I though that I would bring this up here as it seems like a simple fix.
Here is one stack overflow answer with a fix - This should work as it was added by react native to differentiate between itself and node - discussed here
if (typeof document != 'undefined') {
// I'm on the web!
}
else if (typeof navigator != 'undefined' || navigator.product == 'ReactNative') {
// I'm in react-native
}
else {
// I'm in node js
}
So we could make it something like:
if (typeof document != 'undefined' && navigator.product == 'ReactNative') {
// I'm in browser or react-native
isBrowser=true
}
else {
// I'm in node js
isBrowser=false
}
Or another way to do it would be to check whether it has the node only default modules like http that could also be another good way to differentiate
I'm also running into this issue
Seems like a lot of people are having this issue. Having the same problem within my own personal project.
Should be fixed now in v1.0.6
. Can anyone give this a try?
Yep will try in my next build, thank you for the support!
@josdejong Hi, I have tried version 1.0.6 and 1.0.7 and I am still seeing the "Unable to resolve module 'http' from 'node_modules/timesync/lib/request/request.node.js". I have node installed locally and am running react-native 0.61.4. Let me know if you have any idea why it might still be error-ing out or if you know of any workarounds. Thanks..
@daaashleywch I think you're loading the node.js version, can you try to load the browser bundle, from the /dist
folder?
@daaashleywch I think you're loading the node.js version, can you try to load the browser bundle, from the
/dist
folder?
@josdejong
any chance you could expand on what loading from /dist would look like? Also, any chance this could be due to install/configure with yarn instead of npm?
@josdejong for anyone that comes across this, changing "main" to "./dist/timesync.js" in the main package.json file seemed to solve my problem.
@RossmacD I've added fields "browser"
and "react-native"
to the package.json, referring to the bundled file. Can you give timesync@1.0.8
a try to see if that solves the issue?