node.js - React-Native 无法解析模块

标签 node.js socket.io react-native

我收到以下错误:

Dec 16 20:03:26 NathanHyland Bunny[21876]: Unable to resolve module socket.io-client/socket.io from /Users/nhyland/Documents/react-native/Bunny/index.ios.js: Invalid directory /Users/node_modules/socket.io-client/socket.io Dec 16 20:03:26 NathanHyland syslogd[21710]: ASL Sender Statistics

我的 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 模块中看到它。

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

最佳答案

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

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

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

关于node.js - React-Native 无法解析模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325946/

相关文章:

javascript - 访问 Node-Red websocket 监听器

javascript - 无法让 nodejs socket.io 在我的 ubuntu 虚拟机上工作

javascript - socket.io 客户端 - 如果没有连接或服务器关闭,则不会触发回调

node.js - 需要 Engine.IO 教程

node.js - 用 Jest 模拟 AWS SES

node.js - Mongoose 如何在 update/findOneAndUpdate 时验证类型和值

node.js - Vue Node.js/express 应用程序错误无法设置未定义的属性渲染

css - 有没有更好的方法可以在柔性显示中的元素之间保持相同的间隙空间?

react-native - 缩放后在父 View 的边界内平移 Animated.View

javascript - React Native SafeAreaView 背景颜色 - 如何为屏幕顶部和底部分配两种不同的背景颜色?