4

I'm trying to import the Ethereum web3.js library into a React Native project.

I've followed the React Linking Libraries instructions, installing the web3.js package and linking it with the commands:

$ npm install web3 --save
$ react-native link

My index.ios.js file looks as follows:

import { default as Web3 } from 'web3';

import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';

class ReactProject extends Component {
  render() {
    return (
      <Text>Hello world!</Text>
    );
  }
}

AppRegistry.registerComponent('ReactProject', () => ReactProject);

The error message I receive in the simulator when I run the app {"type":"InternalError","message":"react-packager has encountered an internal error, please check your terminal error output for more details"} is not very helpful as the terminal shows only the same message.

How do I go about importing this library, into other projects such as React-Native?

  • I think the actual error message is missing here. As it says you need to dig deeper from terminal, logs, wherever where the actual error message is printed out. – Mikko Ohtamaa Mar 10 '17 at 10:00
  • After restarting the simulator and packager I have a new error about a missing module, which I've resolved by editing the require statement in https://github.com/debris/bignumber.js/blob/master/bignumber.js. I've posted an issue with fix details here: github.com/ethereum/web3.js/issues/576. Does anybody know why web3 uses the debris/bignumber.js fork of the MikeMcl/bignumber.js module? – matthew Mar 10 '17 at 10:19
1

Fixed: https://github.com/ethereum/web3.js/issues/576

Edit The Following File

/node_modules/bignumber.js/bignumber.js

edit as follows:

-if ( !crypto ) try { crypto = require('crypto'); } catch (e) {}
+if ( !crypto ) try { crypto = require('crypto-js'); } catch (e) {} 

Web3 Version: 0.18.2

Import in respective file

var Web3 = require('web3'); or import { default as Web3 } from 'web3';
1

I was able to make it work on web3 version 0.x with web3 version 0.20.0, react 16 fiber

For more info see the following sample app.

0

https://github.com/markspereira/react-native-web3-boilerplate I managed to get web3 1.0.0 working. For account creation I use 'ethereumjs-wallet' as it doesn't work with the web3 1.0.0 on react-native and that pretty much covers everything you would want to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.