DEV Community

Peng
Peng

Posted on

How to use nodejs libs in react native?

React native is a different javascript running environment from browser and nodejs. If a library works in the browser or nodejs, it doesn't necessarily mean it will work in react native.

Recently I built an app called PreCloud: Encrypt before upload, with which you can encrypt texts and files, then you can upload the encrypted texts and files to wherever you want, making any cloud provider an encrypted storage. It's free and open source. Give it a try :)

I used openpgpjs to do all the encryption and decryption. It works perfectly in the browser and nodejs, but not in react native :(

There is a react-native-openpgp, but it's not very popular and seems under heavy development still.


While I read the source code of the Filen app, I came across nodejs-mobile-react-native.

The project will build a nodejs into your app, then you can use almost all nodejs libraries in your react native app, it's very cool.

The nodejs part lives in a nodejs-assets folder, and it communicates with react native through events.

Very easy to setup, check their readme, or see how it works in my app.


Drawback:

  1. It will make your app size much bigger. For a normal app, the apk or the iOS build can be 80+Mb. (The android aab file will be smaller though, 20+Mb). But it's so helpful, I accept it;
  2. It only supports nodejs 12 for now. But they are looking to upgrade it to node 16;

And if you are interested, try my PreCloud app, to protect your privacy.
Play store: https://play.google.com/store/apps/details?id=com.precloud
App store: https://apps.apple.com/us/app/precloud/id1638793841

Like I said in the beginning, it's free, open source, and has no tracking and no server.

Top comments (0)