【问题标题】:React-Native Unable to resolve moduleReact-Native 无法解析模块
【发布时间】:2016-03-23 09:52:12
【问题描述】:

我收到以下错误:

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 12 月 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 中的文件夹/文件,但不起作用,我尝试了不同的导入方式,没有 bueno。

它通过 npm install socket.io-client 安装得很好,我可以在 node-modules 中看到它。

知道为什么它不让我导入吗?

【问题讨论】:

    标签: node.js socket.io react-native


    【解决方案1】:
    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】:

      对于将来遇到此问题的人,请尝试在您的 require 路径中包含“dist”文件夹,如下所示:

      var io = require('socket.io-client/dist/socket.io');
      

      请参阅讨论此here 的 github 问题。

      【讨论】:

        【解决方案3】:
        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';
        

        【讨论】:

          猜你喜欢
          • 2017-10-11
          • 1970-01-01
          • 2018-11-18
          • 1970-01-01
          • 2020-04-15
          • 2020-03-02
          • 2016-12-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多