【问题标题】:React native throws an error inside try {}React Native 在 try {} 中抛出错误
【发布时间】:2021-09-17 15:29:43
【问题描述】:

我将RNUI 安装到一个新的世博会项目中。简单地导入模块后,它会抛出这个错误:

Error: Problem validating fields in app.json. See https://docs.expo.io/workflow/configuration/
 • should NOT have additional property 'nodeModulesPath'.
Failed building JavaScript bundle.
Unable to resolve module react-native-haptic-feedback from /Users/xx/Documents/yy/zz/node_modules/react-native-ui-lib/src/optionalDependencies/HapticFeedbackPackage.js: react-native-haptic-feedback could not be found within the project.

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
  2 |
  3 | try {
> 4 |   HapticFeedbackPackage = require('react-native-haptic-feedback').default;
    |                                    ^
  5 | } catch (error) {}
  6 |
  7 | export default HapticFeedbackPackage;

在我看来,它在 try {} 语句中引发错误似乎非常不合逻辑。相同的模块在旧的 Expo 项目(旧版本)中运行良好。

【问题讨论】:

    标签: javascript reactjs react-native expo try-catch


    【解决方案1】:

    你需要安装react-native-haptic-feedback这个包,然后对它不起作用执行下面给出的步骤。

    删除项目目录中的 node_modules 文件夹,关闭 Metro 捆绑器以及为该项目运行的所有内容。现在运行 npm install 它应该可以工作了。

    【讨论】:

    • 您好,感谢您提出答案。这里的问题是该模块包含许多可选的依赖项,并且对每个依赖项都抛出相同的错误。我尝试删除 node_modules、更改版本等,但这些都不起作用
    • 您好,您的安装过程正确吗?
    • 嗨 Mihir,是的,我已经按照文档安装了它。
    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多