【问题标题】:React-native importing throws 'Unable to resolve module', even though IDE finds it prefectlyReact-native 导入抛出“无法解析模块”,即使 IDE 完美地找到它
【发布时间】:2018-07-19 12:22:31
【问题描述】:

这是我在 App.js 中使用的测试导入

import {test} from './utils/configs';

这是我的 ./utils/configs:

export const test = 'test-string'

App.js 和 Utils 文件夹在同一级别。 IDE 完美找到导入,但 Xcode 模拟器抛出错误: 无法从 /Users/riku/Documents/StreamrLabs/streamr-ios-location-poc/App.js 解析模块 ./utils/configs:无法从 /Users/riku/Documents/StreamrLabs/streamr-ios-location-poc/App.js 找到模块 ./utils/configs。事实上,这些文件都不存在:

  • /Users/User/Documents/Folder/projectname/utils/configs(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)
  • /Users/User/Documents/Folder/projectname/utils/configs/index(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)

似乎导入新模块会破坏 Xcode/bundler。我的应用是独立的 expo 应用。

【问题讨论】:

  • 嗨!您的配置文件的名称是什么?您能否向我们发送屏幕截图/解释您的文件夹树?然后,检查错误信息:这些文件都不存在:/Users/User/Documents/Folder/projectname/utils 与上面的路径不同
  • 我添加了我的文件夹结构的图像。我强烈认为这是 expo/xcode 环境的问题。
  • 尝试重命名 config.js 中的配置:Xcode 正在尝试查找 .js 文件,而这里没有扩展名!
  • 我也试过 {test} 和 test 两种方式导入。
  • 你应该在导出某些东西时使用 {test} 但不是默认的。如果您使用的是export default test;,那么您可以使用import test from ...。使用export test,您必须使用import {test} from ...

标签: xcode react-native expo


【解决方案1】:

文件应该有扩展名! (本例中为 configs.js)。

Xcode 正在尝试查找 .js 文件,而这里没有扩展名!

(记录解决方案)

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2020-07-06
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2017-10-11
    • 2016-03-23
    相关资源
    最近更新 更多