简体   繁体   English

React Native在新的初始化应用程序上找不到模块./index

[英]React Native can't find module ./index on fresh initialized app

I issued react-native init MyApp and react-native run-android 我发布了react-native init MyAppreact-native run-android

Metro server started but when phone request data from it, it crashed giving Metro服务器启动,但是当电话请求数据时,它崩溃了

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:

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? 机器具有新安装的节点npm及其模块,因此没有缓存问题,但是,什么阻止了反应本机提供,甚至无法一窥呢?

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

Metro server instance is started by runAndroid.js from @react-native-community module if asked for react-native run-android 如果要求提供react-native run-androidrunAndroid.js @react-native-community模块的runAndroid.js启动Metro服务器实例

Problem is with Working Directory , Metro instance is launched with wrong working directory and no projectRoot is passed in launchPackager.bat 问题在于工作目录 ,使用错误的工作目录启动了Metro实例,并且在launchPackager.bat未传递任何项目projectRoot目录

Threre are two fixes for this issue, apply only one of the below Threre是针对此问题的两个修复程序,仅适用以下一种

Update node_modules\\react-native\\scripts\\launchPackager.bat file . 更新node_modules\\react-native\\scripts\\launchPackager.bat file

@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

We are giving project root path to Metro instance here via projectRoot argument, 我们通过projectRoot参数将项目根路径提供给Metro实例,

Or in \\node_modules\\@react-native-community\\cli\\build\\commands\\runAndroid\\runAndroid.js edit this , 或在\\node_modules\\@react-native-community\\cli\\build\\commands\\runAndroid\\runAndroid.js编辑此代码

const procConfig = {

    // delete this line    
    cwd: scriptsDir

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

};

We are launching Metro Server with working directory to our project root 我们正在启动带有工作目录的Metro Server到我们的项目根目录

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() . 有关更多信息,请参见\\node_modules\\@react-native-community\\cli\\build\\commands\\runAndroid\\ranAndroid.js startServerInNewWindow()函数,它将通过react-native目录而不是spawn()第三个参数传递项目根目录。

It worked, hope this will help you as well 它奏效了,希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM