How to use node modules with React Native

时间:2015-07-09 15:40:19

标签: javascript node.js reactjs react-native node-modules

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

所以我进入项目文件夹并运行npm install swisseph

在应用源代码中我添加了这个:

var swisseph = require ('swisseph');

我运行了该项目并收到此错误:

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

如何在React Native中使用节点模块?

index.io.js文件包含以下代码:

    /**
 * 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);

1 个答案:

答案 0 :(得分:1)

我会尝试使用chrome调试器运行它。在您到达之前可能存在导致JS崩溃的问题:

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

打开chrome debugging命令+ d,然后单击Chrome中的Debug。这应该打开chrome,其中包含有关如何查看控制台输出的说明。