preston
preston

Reputation: 4367

How to use node modules with React Native

I'm new to React Native and would like to use a node module inside the React Native app I'm playing around with.

The node module I would like to add is this: https://www.npmjs.com/package/swisseph

So I went inside the project folder and ran npm install swisseph

In the app source code I added this:

var swisseph = require ('swisseph');

I ran the project and I get this error:

Invariant Violation: Application AwesomeProject has not been registered.
This is either due to a require() error during initialisation or failure to call AppRegistry.registerComponent.

How does one use node modules inside React Native?

The index.io.js file has these code:

    /**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';

var React = require('react-native');
var swisseph = require ('swisseph');

var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var AwesomeProject = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

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

Upvotes: 1

Views: 4924

Answers (1)

Bob9630
Bob9630

Reputation: 953

I would try running this with chrome debugger on. There might be some problem that is causing your JS to crash before you get to:

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

To open chrome debugging hit command+d and then click on Debug in Chrome. This should open chrome with instructions on how to see the console output.

Upvotes: 1

Related Questions