1

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);
3
  • What's your index.ios.js look like? Commented Jul 9, 2015 at 18:04
  • Hi, I added the code inside index.ios.js above...thank you... Commented Jul 9, 2015 at 18:50
  • 1
    It says AwesomeProject not swisseph so the particular error might be not be caused by the newly added module. try solution presented here stackoverflow.com/questions/29287987/… this worked for me when i encountered the has not been registered error. Commented Jul 10, 2015 at 4:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, yes...it is the var swisseph = require ('swisseph'); that is causing it to crash. That is the node module I would like to add to my project. How do I add it properly?
After looking at swisseph, you might be out of luck. It looks as though as part of it's installation it uses "node-gyp rebuilt" to compile C++ code for it's JavaScript to use. React-Native unfortunately doesn't run a nodejs server on your iPhone and so you really don't have access to all the glory which is nodejs. Your app is crashing because it can't find the necessary native bindings. One option you do have is to add the swisseph C++ code to your project making sure to expose the necessary binding for React-Native to use. See moduscreate.com/react_native_custom_components_ios

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.