timotejroiko / sweph

The definitive Swiss Ephemeris bindings for Node.js
Other
93 stars 15 forks source link

react native compatibility #6

Closed codelocksdev closed 2 years ago

codelocksdev commented 2 years ago

This probably isn't a bug as much as a feature request. I'm running into issues trying to use this package within an expo react native app. I think it's probably got something to do with it being a node binding, and react native doesn't necessarily work out of the box with node core modules.

I ran into this article and it looks like possibly there might be workarounds, but it's a little over my head to figure out. Is there any way to modify this package so that it could be imported into a react native app? https://gist.github.com/parshap/e3063d9bf6058041b34b26b7166fd6bd

Right now I'm stuck with running an external server to use the java swisseph package and the API calls are killing my performance, so I'd love to get the calculations local on the device. Thanks so much!

edit for more info:

this is the (at least initial) error I run into when trying to use the module

Unable to resolve module ./build/Release/sweph.node from D:\dev\repos\swisseph-test-app\node_modules\sweph\index.js: 

None of these files exist:
  * node_modules\sweph\build\Release\sweph.node(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  * node_modules\sweph\build\Release\sweph.node\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  1 | "use strict";
  2 |
> 3 | const sweph = require("./build/Release/sweph.node");
    |                        ^
  4 | sweph.constants = require("./constants.js");
  5 | sweph.sweph = sweph;
  6 | sweph.default = sweph;
krisztianb commented 2 years ago

This is essentially the same thing as #5. The module is using a C++ library which basically does not run on the browser.

timotejroiko commented 2 years ago

To expand a little on the above, this library uses the NAPI which is a C/C++ <-> JS compatibility layer specific for node.js

React Native is a completely different beast altogether, it uses compatibility layers specific for each platform, Java <-> JS for Android and Objective-C <-> JS for iOS.

Its not simply a matter of adding support for it, it would require recreating the entire library to use the compatibility layers specific for those platforms.

Instead, you can try using https://github.com/linchCN/react-native-swisseph which appears to be exactly what you're looking for.

codelocksdev commented 2 years ago

Thanks for the explanation, and for finding that alternative! That does look like what I need.