React-Native 无法解析模块

4

我遇到了以下错误:

Dec 16 20:03:26 NathanHyland Bunny[21876]: 无法从 /Users/nhyland/Documents/react-native/Bunny/index.ios.js 解析模块 socket.io-client/socket.io: 目录 /Users/node_modules/socket.io-client/socket.io 无效 Dec 16 20:03:26 NathanHyland syslogd[21710]: ASL 发送者统计信息

我的 index.io.js 文件:

var React = require('react-native');
var {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableHighlight
    } = React;

import './UserAgent';
window.navigator.userAgent = "react-native";
var _ = require('lodash');
var io = require('socket.io-client/socket.io');

class Bunny extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        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>
                <TouchableHighlight onPress={() => {

      }}><Text>Test</Text></TouchableHighlight>
            </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('Bunny', () => Bunny);

我尝试过直接链接node_modules中的文件夹/文件,但无法正常工作;我尝试了不同的导入方式,但也不行。
通过npm install socket.io-client安装是没有问题的,我可以在node_modules中看到它。
有任何想法为什么不能让我进行导入吗?
3个回答

2
import io from 'socket.io-client';

package.json文件

"dependencies": {
    "react": "16.0.0-alpha.12",
    "react-native": "0.47.1",
    "socket.io-client": "2.0.3"
}

2
对于未来遇到这个问题的人,尝试将“dist”文件夹包含在您的require路径中,如下所示:
var io = require('socket.io-client/dist/socket.io');

请查看讨论此问题的Github问题 这里


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

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

=============================================

import React, {Component} from 'react';

import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight
} from 'react-native';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接