React Native 在新初始化的应用程序上找不到模块 ./index [英] React Native can't find module ./index on fresh initialized app

查看:39
本文介绍了React Native 在新初始化的应用程序上找不到模块 ./index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了 react-native init MyAppreact-native run-android

Metro 服务器启动了,但是当电话从它请求数据时,它崩溃了

Metro server started but when phone request data from it, it crashed giving

Error: Unable to resolve module `./index` from `\node_modules\react-native\scripts/.`: The module `./index` could not be found from `\node_modules\react-native\scripts/.`. Indeed, none of these files exist:

机器新安装了 node、npm 和它的模块,所以没有缓存问题,但是是什么阻止了 React Native 的第一眼?

Machine has newly installed node, npm and it's modules so no cache issues, but then what is preventing react native to give even its first glimpse?

推荐答案

解决方案 Github 参考:#23908(评论)

Metro 服务器实例由 @react-native-community 模块中的 runAndroid.js 启动,如果要求 react-native run-android

Metro server instance is started by runAndroid.js from @react-native-community module if asked for react-native run-android

问题在于工作目录,Metro 实例以错误的工作目录启动,并且launchPackager.bat 中没有传递projectRoot

Problem is with Working Directory, Metro instance is launched with wrong working directory and no projectRoot is passed in launchPackager.bat

此问题有两个修复程序,仅应用以下之一

Threre are two fixes for this issue, apply only one of the below

更新node_modules\react-native\scripts\launchPackager.bat 文件.

@echo off
title Metro Bundler
call .packager.bat

:: delete this line
node "%~dp0..\cli.js" start 

:: Add this line
node "%~dp0..\cli.js" start --projectRoot ../../../ 

pause
exit

我们在这里通过 projectRoot 参数为 Metro 实例提供项目根路径,

We are giving project root path to Metro instance here via projectRoot argument,

或者在 \node_modules\@react-native-community\cli\build\commands\runAndroid\runAndroid.js 中编辑这个

Or in \node_modules\@react-native-community\cli\build\commands\runAndroid\runAndroid.js edit this,

const procConfig = {

    // delete this line    
    cwd: scriptsDir

    // add this line
    cwd: process.cwd()

};

我们正在启动带有工作目录到项目根目录的 Metro Server

We are launching Metro Server with working directory to our project root

更多信息参见\node_modules\@react-native-community\cli\build\commands\runAndroid\ranAndroid.js中的startServerInNewWindow()函数,它是在 spawn() 的第三个参数中传递 react-native 目录而不是 project root.

For more info see startServerInNewWindow() function in \node_modules\@react-native-community\cli\build\commands\runAndroid\ranAndroid.js, it is passing react-native directory rather than project root in third argument of spawn().

成功了,希望对你也有帮助

It worked, hope this will help you as well

这篇关于React Native 在新初始化的应用程序上找不到模块 ./index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆