Skip to content

Does not work in react-native

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