【问题标题】:Unknown named module in React Native when trying to make module optional尝试使模块可选时,React Native 中的未知命名模块
【发布时间】:2020-07-09 18:14:31
【问题描述】:

这是我之前的问题:

Unable to resolve module in React Native - however I want to make the module optional

这是我被指出的资源:

How to require module only if exist. React native

这是我当前的代码:

let GoogleSignin;
const GoogleSigninInit = '@react-native-community/google-signin';
try {
  GoogleSignin = require.call(null, GoogleSigninTest);
} catch (e) {
  console.log("Google Signin is not found");
}

let FBSDK;
const FBSDKInit = 'react-native-fbsdk';
try {
  FBSDK = require.call(null, FBSDKInit);
} catch (e) {
  console.log("Facebook SDK is not found");
}

let InAppBrowser;
const InAppBrowserInit = 'react-native-inappbrowser-reborn';
try {
  InAppBrowser = require.call(null, InAppBrowserInit);
} catch (e) {
  console.log(e);
  console.log("InApp Browser is not found");
}

let AppleAuth;
const AppleAuthInit = '@invertase/react-native-apple-authentication';
try {
  AppleAuth = require.call(null, AppleAuthInit);
} catch (e) {
  console.log("Apple auth is not found");
}

但是,当我尝试使用绝对位于我的 node_modules 文件夹中的react-native-inappbrowser-reborn 时,我收到以下错误:

Unknown named module: "react-native-inappbrowser-reborn"

所以我的代码肯定已经摆脱了可选模块的错误,但它似乎并没有真正加载它们。

有没有办法实际加载我在 node_modules 中的模块?

编辑:

这是此特定模块的完整堆栈跟踪,但同样,其他模块也会发生这种情况。

Unknown named module: "react-native-inappbrowser-reborn"
- node_modules/metro/src/lib/polyfills/require.js:95:12 in metroRequire
- node_modules/react-native-keycloak-social-login/src/Login.js:25:25 in <global>
- node_modules/metro/src/lib/polyfills/require.js:321:4 in loadModuleImplementation
- node_modules/metro/src/lib/polyfills/require.js:201:20 in guardedLoadModule
- node_modules/metro/src/lib/polyfills/require.js:128:6 in metroRequire
- node_modules/metro/src/lib/polyfills/require.js:657:4 in runUpdatedModule
- node_modules/metro/src/lib/polyfills/require.js:532:23 in metroHotUpdateModule
- node_modules/metro/src/lib/polyfills/require.js:53:15 in define
* http://127.0.0.1:19001/node_modules/react-native-keycloak-social-login/src/Login.bundle?platform=ios&dev=true&minify=false&modulesOnly=true&runModule=false&shallow=true:1:1 in eval
- node_modules/metro/src/lib/bundle-modules/injectUpdate.js:65:4 in inject

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    根据README,安装不是完全自动的,您需要根据平台和react-native版本进行进一步的处理。

    如果您已按照上述操作,请尝试删除您的 node_modules,运行 npm cache clean 并重新安装 node_modules 并再次运行该应用程序。

    【讨论】:

    • 所有其他模块也会发生这种情况,包括不需要额外安装步骤的模块
    • 例如,我用这个模块试了一下,也遇到了同样的问题:github.com/alexbrillant/react-native-deck-swiper
    • 您是否尝试在清理 npm 缓存并再次运行应用程序后重新安装 node_modules?
    • 刚刚做了,同样的问题。同样,这是来自一个特定模块的自述文件。我尝试了多个模块并遇到了相同的错误。这不是由于这个特定模块的问题。
    • 导入后您在哪里以及如何使用这些模块?代码中的哪一行给出了错误?
    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多