修复错误:无法从“ ”解析模块“./index.android”

Jim*_*Jim 6 react-native react-navigation

尝试在 android 上使用 react-navigation 构建 react-native v0.61.5 应用程序时遇到此错误。iOS 运行良好,不知道为什么我会收到这个 index.android 文件错误,因为我认为 react-native 合并的索引文件只是index.js作为入口点的单数。

这是完整的错误:

加载依赖图,完成。错误:无法./index.android从``解析模块 :

16) 在 /Users/name/Desktop/Development/app/node_modules/metro/src/lib/transformHelpers.js:267:42 在服务器。(/Users/name/Desktop/Development/app/node_modules/metro/src/Server.js:1088:41) at Generator.next () at asyncGeneratorStep (/Users/name/Desktop/Development/app/node_modules/metro/ src/Server.js:99:24) 在 _next (/Users/name/Desktop/Development/app/node_modules/metro/src/Server.js:119:9)

这是 index.js:

import { AppRegistry } from 'react-native';
import App from './App';
import 'react-native-gesture-handler';
import { name as appName } from './app.json';

AppRegistry.registerComponent(appName, () => App);
Run Code Online (Sandbox Code Playgroud)

包.json:

{
    "name": "appName",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "android": "react-native run-android",
        "ios": "react-native run-ios",
        "start": "react-native start",
        "test": "jest",
        "lint": "eslint ."
    },
    "dependencies": {
        "algoliasearch": "^4.0.3",
        "moment": "^2.24.0",
        "moment-timezone": "^0.5.27",
        "react": "16.9.0",
        "react-instantsearch-native": "^6.3.0",
        "react-native": "0.61.5",
        "react-native-agora": "^2.9.1-alpha.2",
        "react-native-algolia-dropdown": "^1.6.0",
        "react-native-calendars": "^1.220.0",
        "react-native-chart-kit": "^4.3.0",
        "react-native-code-push": "^6.0.0",
        "react-native-extended-stylesheet": "^0.12.0",
        "react-native-firebase": "^5.6.0",
        "react-native-gesture-handler": "^1.5.2",
        "react-native-image-crop-picker": "^0.26.1",
        "react-native-material-dropdown": "^0.11.1",
        "react-native-reanimated": "^1.4.0",
        "react-native-safe-area-context": "^0.6.2",
        "react-native-screens": "^1.0.0-alpha.23",
        "react-native-snap-carousel": "^3.8.4",
        "react-native-splash-screen": "^3.2.0",
        "react-native-svg": "^9.13.6",
        "react-native-view-shot": "^3.1.2",
        "react-navigation": "^4.0.10",
        "react-navigation-drawer": "^2.3.3",
        "react-navigation-stack": "^1.10.3",
        "react-redux": "^7.1.3",
        "redux": "^4.0.4",
        "redux-persist": "^6.0.0",
        "redux-thunk": "^2.3.0",
        "rn-fetch-blob": "^0.11.2",
        "tipsi-stripe": "^7.5.1"
    },
    "devDependencies": {
        "@babel/core": "7.7.5",
        "@babel/runtime": "7.7.6",
        "@react-native-community/eslint-config": "0.0.5",
        "babel-jest": "24.9.0",
        "eslint": "6.7.2",
        "jest": "24.9.0",
        "metro-react-native-babel-preset": "0.56.3",
        "react-test-renderer": "16.9.0"
    },
    "jest": {
        "preset": "react-native"
    }
}
Run Code Online (Sandbox Code Playgroud)

项目结构 在此处输入图片说明

图书馆已正确集成,有什么建议吗?

Ris*_*ahu 6

如果您的项目中有 index.ios,它会抛出错误,因为它无法在您的项目根目录中找到 index.android 文件

您可以创建 index.android.js 文件并在该文件中导入原始 index.js 文件并检查它是否有效(快速修复)

所以基本上在 React v0.49 之后,你不需要 index.ios.js 和 index.android.js。你只需要 index.js:

但是出现这个问题有两种情况

  • 如果您从旧项目进行更新,您需要确保从本机代码加载正确的文件以采用新的单文件入口点
  • 其次是本机反应的不一致行为,因为有些人由于守望者或缓存而在较新版本中随机遇到此问题

第一种情况的解决方案

//For android in this directory you need to make this change android/app/src/main/java/<yourPackage>/MainApplication.java

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
  //...
  @Override
  protected String getJSMainModuleName() {
    return "index";
  }
};
Run Code Online (Sandbox Code Playgroud)

//对于这个目录中的ios ios//AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  //...
}
Run Code Online (Sandbox Code Playgroud)

这两种解决方案在某些情况下也有效

  • watchman watch-del-all && rm -rf package-lock.json && rm -rf node_modules && rm -rf $TMPDIR/metro-* && rm -rf $TMPDIR/haste-map-*

或者

  • 删除 index.ios.js 和 index.android.js 文件并创建单个 index.js

在某些情况下,很难确定为什么有人会遇到此问题。