[Ethereum] Using Web3 1.0 with React Native

web3js

This question has already been asked but has never received a clear answer. Plus, the questions are old and in the blockchain space, a one-year old question is obsolete. This question is for React Native and version 0.51 and web3 version 1.0-beta

Hi,

I am trying to create a React Native application that uses web3js but I am unable to actually import it. The error is pretty obvious but I don't know how to solve it.

attempted to import the Node standard library module "crypto". It failed because React Native does not include the Node standard library.

Is the error due to Expo? Should I eject? Have anyone actually solved this?

Thanks,
Dario

Best Answer

There is some documentation about React Native that will help you understand why some packages are not possible to be included into React Native applications.

if the package depends on Node standard library APIs, you will not be able to use it with Expo. The Node standard library is a set of functionality implemented largely in C++ that exposes functions to JavaScript that aren't part of the JavaScript language specification, such as the ability to read and write to your filesystem. React Native, and by extension Expo, do not include the Node standard library, just like Chrome and Firefox do not include it. JavaScript is a language that is used in many contexts, from mobile apps (in our case), to servers, and of course on websites. These contexts all include their own runtime environments that expose different APIs to JavaScript, depending on what makes sense in the context. ref

That said, you can try to include the dist files instead of the package. When you include the package via Nodejs, it will be compiled (if it's required) and possibly use some Node standard libraries.

You can find the web3 dist here, and since it's pure JavaScript (that will run in the browser), it should work also in React Native, according to this page.