React Native:无法解析模块 [英] React Native: Unable to Resolve Module

查看:108
本文介绍了React Native:无法解析模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very simple React Native project that I am trying to get working. It is an iOS project that simply adds a RCTRootView to a UIViewController. When I hit the url from a web browser I get:

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>
    )
  }
}

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

I am at a loss on how to resolve this. I have tried all of the following:

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

有没有人看到我的代码有什么问题,或者知道可能是什么问题?我愿意通过电子邮件或电话交谈来解决这个问题.我快绝望了.

Does anyone see anything that is wrong in my code or know what the issue might be? I'd be willing to converse over email or phone to solve this. I am getting desperate.

推荐答案

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

import HelloReact from '/src/components/HelloReact'; searches for module in absolute path, not relative to current(rn app) directory and so rn couldn't find that (as it doesn't exists)

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

这篇关于React Native:无法解析模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆