React native :Unable to resolve module 实际上,这些文件都不存在:

问题描述 投票:0回答:6

我正在关注 this medium article 在我的 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,
  },
});

当我尝试在里面使用

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:

谁能帮我解决这个问题

如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。

android reactjs react-native components custom-component
6个回答
23
投票

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

npm start --clean-cache

8
投票

您从

HomeScreen
目录中的
screens
组件引用它。因为你使用的是本地的./路径,所以它试图在
screens/customComponents
中找到它。使用
../customComponents/floating_title_text_input_field
应该修复它。


4
投票

即使您的错误是在

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

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

0
投票

FloatingTitleTextInputField 组件似乎是一个命名导出。所以像

import { FloatingTitleTextInputField } from 'the source'
一样导入它。

或者简单地默认导出FloatingTitleTextInputField

export default class FloatingTitleTextInputField
floating_title_text_input_field.js文件中。


0
投票

我参加聚会可能有点晚了,但是如果有人仍在寻找这个问题并且没有找到解决方案并且正在使用 Expo,请确保您从中得到错误的组件是否具有

import { StatusBar } from 'expo-status-bar';

return (
<View>
...Some Code
<StatusBar style="auto" />
<View>
)

在 return 语句中出于某种原因从 return 块中删除此导入将破坏您的代码!


0
投票

对我来说,这是由于自动导入的大写字母错误。

从'./screens/HomeScreen'导入{HomeScreen};

从'./screens/homeScreen'导入{HomeScreen};

© www.soinside.com 2019 - 2024. All rights reserved.