修复错误:无法从''

时间:2020-03-09 09:47:19

标签: react-native react-navigation

尝试在Android上构建带有react-navigation的react-native v0.61.5应用程序时遇到此错误。 iOS运行正常,不确定为什么会收到此index.android文件错误,因为我认为本机将合并的索引文件合并为单个index.js作为入口点。

这是完整的错误:

已完成加载依赖关系图。错误:无法解析模块 ./index.android来自“:

这些文件都不存在:* index.android(.native | .android.js | .native.js | .js | .android.json | .native.json | .json | .android.ts | .native.ts | .ts | .android.tsx | .native.tsx | .tsx) * index.android/index(.native | .android.js | .native.js | .js | .android.json | .native.json | .json | .android.ts | .native.ts | .ts |。 android.tsx | .native.tsx | .tsx) 在ModuleResolver.resolveDependency(/Users/name/Desktop/Development/app/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:163:15) 在ResolutionRequest.resolveDependency(/Users/name/Desktop/Development/app/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:52:18) 在DependencyGraph.resolveDependency(/Users/name/Desktop/Development/app/node_modules/metro/src/node-haste/DependencyGraph.js:282:16) 在/Users/name/Desktop/Development/app/node_modules/metro/src/lib/transformHelpers.js:267:42 在服务器上。 (/用户/名称/桌面/开发/应用程序/node_modules/metro/src/Server.js:1088:41) 在Generator.next() 在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);

package.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"
    }
}

项目结构 enter image description here

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

3 个答案:

答案 0 :(得分:4)

抛出错误,因为如果您的项目中有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";
  }
};

//,并且对于此目录中的ios ios // AppDelegate.m

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

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  //...
}

这两种解决方案在某些情况下也可以使用

  • 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

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

答案 1 :(得分:0)

使用cache-clean运行打包程序: npx react-native start --reset-cache

答案 2 :(得分:-1)

它已经为我解决了

react-native link