Search code examples
javascriptimportreact-nativeimporterrorethereum

React-Native cannot import library


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 libraries like this into React Native?


Solution

  • Try this instead to import the library:

    var Web3 = require('web3');