React Native Error: Unable to resolve module `./index` from `` - multiple issues

React Native Error: Unable to resolve module `./index` from `` - multiple issues

我用这个boilerplate。我几乎 React Native 在我的 Android 设备上工作。但不知何故,它在最后一步失败了,因为它没有找到 index.js 文件。但是那个文件位于我的项目中。

所以当我现在 运行 这个在 src/

yarn react-native run-android

这是 Node CLI window:

所以我的问题是:

Error: Unable to resolve module `./index` from ``:

None of these files exist:
  * index(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)

从控制台本身看不出任何问题:

所以由于某种原因它找不到 index.js 文件。

我已经尝试过 yarn react-native start -- --reset-cache 但它没有改变任何东西。

编辑#1:

当我在根文件夹中添加一个假的空 index.js 文件时,错误消失了,在模拟器中我得到了这个:

编辑#2:

我离解决方案越来越近了。我刚刚将其添加到根目录中的假 index.js

import {AppRegistry} from 'react-native';
import App from './src/components/App';
import {name as appName} from './src/app.json';
AppRegistry.registerComponent(appName, () => App);

然后我得到了:

编辑#3:

现在它帮助我用以下内容覆盖 app.json

{
  "name": "test",
  "displayName": "test"
}

新错误是:

编辑#4:

我现在全部移回 src 文件夹。所以在根目录中不再有 index.js 了。现在这很好用。通常的react-scripts start是运行ning。还有 react-native start 运行s。 run-android 的问题已在 MainApplication.java

中修复
protected String getJSMainModuleName() {
  return "index.android";
}

protected String getJSMainModuleName() {
  return "src/index.android";
}

我认为这是位置的另一个问题...

所以主要问题相对容易修复。所有需要做的就是拥有一个占位符 App.js 文件和另一个 index.js 文件。具体怎么做 here。这是简短的描述:

首先将您现有的 App.js 文件移动到 src/App.js

现在在主文件夹中创建一个新的App.js

import React from 'react';
import HybridApp from './src/App';
const App = (props) => {
  return (
    <HybridApp />
  );
}
export default App;

同时创建一个新的 src/index.js 文件:

import React from 'react';
import ReactDom from 'react-dom';
import App from './App';
ReactDom.render(<App />, document.getElementById("root"));

所有其他问题都与使用错误的技术有关。就像你不能在 React Native 中使用 window 一样。这里的代码需要重写才能再次运行。