Using Core Node JS Modules in React Native Apps | Hacker Noon

Sometimes while creating advanced apps in react-native you may want to encrypt the data you are sending to the web for secured web communication. You may also want to encrypt the payment-related data that is sent to the payment gateway.

Let’s say you want to use the crypto module to create some encrypted data. It might seem natural to do something like this:

There are many alternate modules out there namely

expo-crypto , react-native-crypto and many others but none of these have all the methods of crypto module ,especially if you want to use createCipheriv method you wont find it in any of those modules out there.

var crypto = require('crypto');

var mykey = crypto.createCipher('aes-128-cbc', 'mypassword');
var mystr = mykey.update('abc', 'utf8', 'hex')
mystr +='hex');

console.log(mystr); //34feb914c099df25794bf9ccb85bea72

Just import a module and create an encrypted string, right? Right? Nope!

But this doesn’t work, because crypto is a core Node JS module, which means it’s probably C++ code bundled with the Node JS binary, not Javascript. The React Native packager can’t package it[1] along with your app’s Javascript bundle, so you get a runtime error:

Unable to resolve module 'crypto' 

This makes core modules like the crypto, stream, etc, and the thousands of npm modules that depend on them unusable from React Native. Fortunately, there’s a solution to this problem, but it takes some work.


If you’re familiar with the module bundler Browserify, you might know that it allows you to use core Node JS modules like crypto in the browser using polyfills[2]. So let’s try to create a standalone Javascript file that contains a polyfill for the cryptomodule, and use it within our app:

1. First, install browserify: Here we are installing browserify globally. If you have installed it before globally then the below command need not be executed.

npm install -g browserify

2. Create a file


in your root directory ;

In the file


import the module crypto and export it in the same file. Make sure that you do not create




 directory. If you create it in 


 directory this file will be lost every time

 npm install 

is executed.

var crypto = require("crypto");

3. Create a standalone Javascript bundle 


 using browserify.

browserify crypto-in.js -o crypto-custom.js

This creates a file 


, which contains some 26,660 lines of code (containing the polyfills). The last few lines contain our code from 



//26,656 lines above..
  // Our code from crypto-in.js
  var crypto = require("crypto");
  module.exports = crypto;

At this point, it might seem like we’re done, but we’re not! If you replace the line:

const crypto = require('crypto');

in the app code with:

const crypto = require('./crypto-custom');

//here iam in root directory and crypto-custom.js file is also in 

and try to run the app, you get the following error:

Unable to resolve module bn.js

Turns out the word require is given special treatment by the React Native packager, and you can’t redefine it to mean something else, which is what Browserify tries to do within the bundle. If you look carefully inside


, you will notice that it always passes in a custom require to function as a parameter and never actually uses the global require. There is a hacky but simple way to fix this problem, explained below.

4. Open the file 


 in a text editor, and replace all instances of the word require with something else e.g.


. Make sure it’s unique, so it doesn’t conflict with any existing code and mess things up.

If you save


 and reload the app now, the previous error will go away, but a new error will appear:

The packager isn’t able to find the method 


, because crypto isn’t exported properly from 


. If you look at the last few lines of the bundle, you’ll find that this is because we’re setting


 not on the global modules object, but on something else that’s passed in as a function argument:

//26,656 lines above..
var crypto = req("crypto");
module.exports = crypto;

5. To export 


 properly from 


, we need to make a few more changes to the file:

Add the statement

 var crypto

 at the top of the file 


Replace the statement 

var crypto = require('crypto')

; with 

crypto = require('crypto');

 to set the outer variable, instead of creating a local variable inside the function body. Move the line 

module.exports = crypto;

 outside the function body to the bottom of the file, so that it references the global module object.

After the changes, crypto-custom.js should look something like this:

var crypto;  //at top of the file at line 1
// Some 26,656 lines of polyfills..
  crypto = req("crypto");

module.exports = crypto; 

That’s it! If you run the app now, it should work perfectly:


  1. —[2]
  2. —[3]
  3. —


Join Hacker Noon

Create your free account to unlock your custom reading experience.

read original article here