【问题标题】:React Native: Unable to Resolve ModuleReact Native:无法解析模块
【发布时间】:2017-10-11 00:00:16
【问题描述】:

我有一个非常简单的 React Native 项目,我正在尝试开始工作。这是一个简单地将 RCTRootView 添加到 UIViewController 的 iOS 项目。当我从网络浏览器点击网址时,我得到:

Unable to resolve module `/src/components/HelloReact` from `/Users/myusername/Desktop/DemoReact/index.ios.js`: Directory /src/components/HelloReact doesn't exist.

index.ios.js

'use strict';

import { AppRegistry } from 'react-native';
import HelloReact from '/src/components/HelloReact';

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

HelloReact.js

import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';

export default class HelloReact extends Component {
  render() {
    return (
      <Text style={{fontWeight: 'bold'}}>
        I am bold
        <Text style={{color: 'red'}}>
          and red
        </Text>
      </Text>
    )
  }
}

我不知道如何解决这个问题。我已经尝试了以下所有方法:

  • npm start
  • npm start --reset-cache
  • 删除 node_modules 目录并重新安装
  • 卸载并重新安装 node、watchman、react-native 和 npm
  • 在物理设备和模拟器上试用

有没有人发现我的代码有任何问题或知道问题可能是什么?我愿意通过电子邮件或电话交谈来解决这个问题。我越来越绝望了。

【问题讨论】:

    标签: ios objective-c node.js reactjs react-native


    【解决方案1】:

    import HelloReact from '/src/components/HelloReact'; 在绝对路径中搜索模块,而不是相对于当前(rn app)目录,所以 rn 找不到(因为它不存在)

    改成import HelloReact from './src/components/HelloReact';

    【讨论】:

    • 哇!我好蠢。那行得通!非常感谢!运行应用程序时还有另一个问题,但我会编辑这个问题并重新发布一个新问题。
    • Gald 来帮忙 ;) 如果有帮助,请务必将其标记为答案。 ^_^
    • 我会尽快将其标记为答案!太感谢了。下一个问题来了!
    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2020-04-15
    • 2020-03-02
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多