You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-Native报错:Cannot find Module '../../App' 如何解决?

修复Expo项目移动文件到SRC文件夹后的模块找不到问题

报错信息

AppEntry.js:1 Uncaught Error: Cannot find module '../../App'

Uncaught ReferenceError: process is not defined

index.js:1 ./node_modules/expo/AppEntry.js:3
Module not found: Can't resolve '../../App'
  1 | import registerRootComponent from 'expo/build/launch/registerRootComponent';
  2 |
> 3 | import App from '../../App';
  4 |
  5 | registerRootComponent(App);

解决步骤

核心问题修复:修正项目入口路径

默认的AppEntry.js在node_modules/expo目录下,它会向上两级查找App.js,但文件移入src文件夹后这个路径失效。禁止修改node_modules内的文件(会被npm install重置),推荐以下两种方案:

方案1:直接修改package.json的入口指向src/App.js

打开项目根目录的package.json,找到main字段,修改为:

"main": "src/App.js"

保存后重启项目,Expo会直接加载src目录下的App.js文件。

方案2:自定义根目录入口文件(更灵活)

在项目根目录新建index.js,写入以下内容:

import registerRootComponent from 'expo/build/launch/registerRootComponent';
import App from './src/App';

registerRootComponent(App);

然后修改package.json的main字段为:

"main": "index.js"

这种方式方便后续添加初始化逻辑(比如全局状态配置、第三方库初始化)。


附加:修复process is not defined错误

如果修复入口后仍存在这个错误,按以下步骤处理:

  1. 安装依赖:
npm install process
  1. 新建或修改metro.config.js(项目根目录):
const { getDefaultConfig } = require('expo/metro-config');

const config = getDefaultConfig(__dirname);

config.resolver.extraNodeModules = {
  ...config.resolver.extraNodeModules,
  process: require.resolve('process/browser'),
};

module.exports = config;
  1. 重启项目即可。

内容的提问来源于stack exchange,提问作者THIAGO DE BONIS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 13:15:31