1

I'm using some auth node module in my react native application which is using jose as a dependency. I get the following error which I believe is due to the fact that in-build typescript/ node libraries are not supported by React native.

ERROR ReferenceError: Can't find variable: crypto ERROR
Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

Even using node-libs-react-native or rn-nodify in my app didn't fix the issue. It appears adding a resolver to metro.config.js to provide extra node modules doesn't fix the issue for the libraries used by dependencies (here jose is used by the auth library and jose is using crypto).

Any solution or workaround to get this into working?

I found 2 similar issues, but nothing mentioned there worked for me.

  1. Requiring unknown module "crypto" in react-native environment
  2. Unable to resolve module crypto in reactnative
Thamindu DJ
  • 1,679
  • 4
  • 16
  • 29
  • What auth node module you are using? Jose has browser version, which can be accessed using `const * as Jose = require('jose/dist/browser')` – Medet Tleukabiluly Dec 02 '21 at 10:22
  • @MedetTleukabiluly I'm already importing from `jose/dist/browser`, but the issue is with crypto is not being there in react native environment. – Thamindu DJ Dec 02 '21 at 10:27
  • The `crypto` from jose browser runtime is not Node's crypto. It's Web Cryptography API. There are many Web APIs missing in react native, not just webcrypto. Nevertheless if you manage to polyfill all these global variables it might just work. But mind you you'll depend on a crypto layer which is not native to your runtime. D'oh, since react-native doesn't have one. And like i answered [here](https://github.com/panva/jose/discussions/332#discussioncomment-1726497), I have no plans to supporting this javascript runtime - It's not a Web API runtime. It's riddled with polyfills. Staying away. –  Dec 02 '21 at 12:31
  • @FilipSkokan Auth library is already polyfilled and I was previously using jose 3.x version and that worked in react native without throwing this error. However observe some error in `parseJwk` function which was not returning the promise. After updating to 4.x that problem got solved (solved in node.js and web) and ended up in the above error for react native – Thamindu DJ Dec 02 '21 at 15:29

1 Answers1

0

I was able to use the jsrsasign library to get the functionality implemented in the React native environment. This is a library written in pure javascript which provides a lot of crypto related functionalities.

Thamindu DJ
  • 1,679
  • 4
  • 16
  • 29