React Native:无法解析模块确实,这些文件都不存在:

Gok*_*oku 11 android components custom-component reactjs react-native

我正在按照这篇中等文章FloatingTitleTextInputField在我的反应本机项目中使用

下面是我的项目结构

在此输入图像描述

这是我的代码HomeScreen.js

import React, {Component} from 'react';
import {Text, View, TextInput, StyleSheet} from 'react-native';
import FloatingTitleTextInputField from './customComponents/floating_title_text_input_field';



export default class HomeScreen extends Component {
  render() {
    return (
      // <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      //   <Text>My First React App</Text>
      //   <TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}} />
      // </View>

      <View style={styles.container}>
        <View style={styles.container}>
          <Text style={styles.headerText}>Its Amazing</Text>
          <FloatingTitleTextInputField
            attrName="firstName"
            title="First Name"
            value={this.state.firstName}
            updateMasterState={this._updateMasterState}
          />
          <FloatingTitleTextInputField
            attrName="lastName"
            title="Last Name"
            value={this.state.lastName}
            updateMasterState={this._updateMasterState}
          />
        </View>
      </View>
    );
  }
}
var styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 65,
    backgroundColor: 'white',
  },
  labelInput: {
    color: '#673AB7',
  },
  formInput: {
    borderBottomWidth: 1.5,
    marginLeft: 20,
    borderColor: '#333',
  },
  input: {
    borderWidth: 0,
  },
});
Run Code Online (Sandbox Code Playgroud)

FloatingTitleTextInputField当我尝试在内部使用时,HomeScreen.js出现以下错误

    error Unable to resolve module `./floating_title_text_input_field` from `React Native/AwesomeProject/screens/

HomeScreen.js`: The module `./floating_title_text_input_field` could not be found from `/React Native/AwesomeProject/screens/HomeScreen.js`. Indeed, none of these files exist:


  * `/React Native/AwesomeProject/screens/floating_title_text_input_field(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`


  * `/React Native/AwesomeProject/screens/floating_title_text_input_field/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`. Run CLI with --verbose flag for more details.


Error: Unable to resolve module `./floating_title_text_input_field` from `React Native/AwesomeProject/screens/HomeScreen.js`: The module `./floating_title_text_input_field` could not be found from `/React Native/AwesomeProject/screens/HomeScreen.js`. Indeed, none of these files exist:
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决这个问题

如果需要更多信息,请告诉我。提前致谢。您的努力将受到赞赏。

小智 30

您可以清理捆绑器上的缓存:

npm start --clean-cache
Run Code Online (Sandbox Code Playgroud)


Mik*_*e M 9

HomeScreen您从目录中的组件引用它screens。因为您使用的是本地./路径,所以它会尝试在screens/customComponents. 使用../customComponents/floating_title_text_input_field应该可以修复它。

  • @Goku FWIW,我通常不使用相对路径,而是引用项目根目录中的项目,如“myprojectname/customComponents/floating_title_text_input_field”。祝你好运 (2认同)

Dev*_*ein 5

尽管您的错误是在require声明期间使用了错误的路径,但我认为分享我如何解决此问题可能会很有用,其中文件导入路径不是错误的原因。在添加一些图像资源并在我的组件中需要它们后,我遇到了这个问题。但我忘记再次构建应用程序。经过多次尝试,这是对我有用的解决方案。

  1. 停止您的开发服务器。
  2. 使用 . 在您的 Android 设备或模拟器上安装该应用程序yarn android/ios
  3. 使用 启动开发服务器yarn start