【问题标题】:Unable to resolve module for local file (React Native)无法解析本地文件的模块(React Native)
【发布时间】:2018-01-21 23:15:39
【问题描述】:

奇怪的是,React Native 无法解决本地文件导入的错误。

Unable to resolve module `./components/MyComponent" from ".//App.js`: could not resolve `/Users/myusername/Desktop/mylibrary/components/MyComponent' as a file nor as a folder","name":"UnableToResolveError","type":"UnableToResolveError","errors":[{}]},"type":"bundling_error"}"

mylibrary/App.js:

import React from 'react';
import {
  View,
  Text,
  StyleSheet,
  ScrollView,
  AsyncStorage,
} from 'react-native';

import MyComponent from './components/MyComponent';

mylibrary/components/MyComponent.jsx:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default class MyComponent extends React.Component {

    render() {
        <View>
            {this.props.children}
        </View>
    }
}

我尝试将文件重命名为小写 (mycomponent.jsx),但没有任何区别。我也试过重启模拟器,重置缓存,也没有用。

【问题讨论】:

  • 如果我没记错import语句默认尝试导入.js文件,尝试将MyComponent.jsx重命名为MyComponent.js

标签: react-native


【解决方案1】:

import语句默认尝试导入.js文件,尝试将MyComponent.jsx重命名为MyComponent.js

引用MDN

要导入的模块。这通常是包含模块的 .js 文件的相对或绝对路径名,不包括 .js 扩展名。某些捆绑程序可能允许或要求使用扩展;检查你的环境。只允许使用单引号和双引号字符串。

【讨论】:

  • 不幸的是,这也不起作用。 :( 错误变为Unable to resolve module './components/mycomponent.js' from ".//App.js': could not resolve /Users/myusername/Desktop/mylibrary/components/mycomponent' as a file nor as a folder","name":"UnableToResolveError","type":"UnableToResolveError","errors":[{}]},"type":"bundling_error"}"
  • @zahabba 你把文件名改成了小写?确保文件名大小写与您要导入的文件名匹配。
【解决方案2】:

组件错误。您没有在 render 方法中返回任何内容。试试这个:

render() {

return(
        <View>
            {this.props.children}
        </View>
)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 2016-03-23
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2020-04-15
    • 2020-03-02
    相关资源
    最近更新 更多