javascript - React Native 在新初始化的应用程序上找不到模块 ./index
问题描述
我发出react-native init MyApp
和react-native run-android
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:
机器有新安装的节点、npm 和它的模块,所以没有缓存问题,但是是什么阻止了 react native 甚至是它的第一眼?
解决方案
解决方案 Github 参考:#23908(评论)
如果需要, Metro 服务器实例由runAndroid.js
from@react-native-community
模块启动react-native run-android
问题在于工作目录,Metro 实例使用错误的工作目录启动并且没有projectRoot
传入launchPackager.bat
此问题有两个修复程序,仅应用以下其中一个
更新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
我们在这里通过projectRoot
参数提供 Metro 实例的项目根路径,
或者在\node_modules\@react-native-community\cli\build\commands\runAndroid\runAndroid.js
编辑这个,
const procConfig = {
// delete this line
cwd: scriptsDir
// add this line
cwd: process.cwd()
};
我们正在启动带有工作目录的 Metro Server 到我们的项目根目录
有关更多信息,请参阅startServerInNewWindow()
中的函数\node_modules\@react-native-community\cli\build\commands\runAndroid\ranAndroid.js
,它在 的第三个参数中传递react-native
目录而不是项目根目录spawn()
。
成功了,希望对你也有帮助
推荐阅读
- excel-formula - 从excel中的复杂字符串中提取文本
- sql - 某些记录的 SQL 查询失败
- android - 多个片段的基本导航片段
- c - 更改 HP Fortify C 规则优先级
- php - 从没有插件的 URL 中删除父页面?
- javascript - 主题的 localStorage
- javascript - 重新渲染的 HTML 不受 jQuery 功能的影响
- sql-server - 使用数据库部署 ASP.NET MVC Entity Framework Web 应用程序
- c++ - 为什么这个指针值不能转换为整数的规则是什么?
- python - 在Python中以所有可能的方式将两个字符组合在一起,而不需要itertools到一定的长度