解决RN异常Cannot find entry file index.android.js in any of the roots的心累历程

最近接触到React Native技术,感觉会是APP开发的一个方向,就来研究一下,看着官网上写的蛮简单的,还有不少大神的教程,然后初上手还是问题不断...

先贴一下官网的教程,怎么配置上面都写的比较清楚的,当然,看看其他大神写的环境搭建,配合一下理解比较好,像python不需要用的话就不用装了,官网提的Chocolatey也可以不用装

开发环境搭建:https://reactnative.cn/docs/0.51/getting-started.html#content

设备上运行:https://reactnative.cn/docs/0.51/running-on-device-android.html#content

英文官网:http://facebook.github.io/react-native/docs/getting-started.html

我安装的软件版本,除了JDK安装的早,其他都是最新版本:

node: v8.10.0

react-native-cli: 2.0.1

react-native: 0.54.3

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

我创建的工程目录在 F:\react_native,工程名称 FirstRn, 下面用到这些路径的,要替换为自己Project的实际路径

一. 创建好工程后,编译时遇到的问题 FAILURE: Build failed with an exception.
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> The SDK directory 'SDK路径' does not exist.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

这个问题是用 react-native init 命令创建的工程,和用Android studio里面创建的工程相比,缺少了local.properties文件导致,拷贝一个Android studio里面的local.properties文件到F:\react_native\FirstRn\android,再次运行 react-native run-android即可通过。

二. bundling failed: NotFoundError: Cannot find entry file index.android.js in any of the roots 问题

前面都没问题了,用react-native run-android也能启动成功

Scanning folders for symlinks in F:\react_native\FirstRn\node_modules (99ms)
┌──────────────────────────────────────────────────────────────────────────────┐
│                                                                              │
│  Running Metro Bundler on port 8081.                                         │
│                                                                              │
│  Keep Metro running while developing on any JS projects. Feel free to        │
│  close this tab and run your own Metro instance if you prefer.               │
│                                                                              │
│  https://github.com/facebook/react-native                                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Looking for JS files in
   F:\react_native\FirstRn


Metro Bundler ready.

Loading dependency graph, done.

然后,用浏览器去访问http://localhost:8081/index.android.bundle?platform=android时,却出现这个问题,手机上解决红屏后,一片空白,index.js已经载入100%,reload n多次也没用,菜单上一个一个试也没用...

error: bundling failed: NotFoundError: Cannot find entry file index.android.js in any of the roots: ["F:\\react_native\\FirstRn"]
    at DependencyGraph.getAbsolutePath (F:\react_native\FirstRn\node_modules\metro\src\node-haste\DependencyGraph.js:316:11)
    at F:\react_native\FirstRn\node_modules\metro\src\DeltaBundler\DeltaCalculator.js:280:416
    at Generator.next (<anonymous>)
    at step (F:\react_native\FirstRn\node_modules\metro\src\DeltaBundler\DeltaCalculator.js:11:445)
    at F:\react_native\FirstRn\node_modules\metro\src\DeltaBundler\DeltaCalculator.js:11:605
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
 BUNDLE  [android, dev] ./index.android.js ░░░░░░░░░░░░░░░░ 0.0% (0/1), failed.

郁闷,抓狂,然并卵....

问题还是要解决,只能百度,谷歌了......

1.先搜到这篇文章 https://blog.csdn.net/mymy_blog/article/details/73277884 ,试了上面的方法,问题依旧...

2.再看到这篇文章 https://segmentfault.com/q/1010000011930742/a-1020000011980791 ,然后跳到 https://github.com/facebook/react-native/issues/16517,文章写的比较早,检查了里面写的问题,只有一个index.js的问题已经解决了,继续失败...

3.又看了这篇 https://stackoverflow.com/questions/44786655/react-native-cannot-find-entry-file-in-any-of-the-roots,折腾之后,问题依然...

4.卸载babel-preset-react-native重装,又看到这个,运行之后,还得继续...

       yarn remove babel-preset-react-native

yarn add babel-preset-react-native

5.看到有建议运行 npm install 的,运行后,依旧...

6.内心很抓狂有没有,抓狂...

7.没人可指导,没相似问题可参考,怎么办?

还是找官网吧,突然就注意到了一句话

Windows用户请注意,请不要在命令行默认的System32目录中init项目!会有各种权限限制导致不能运行!

然后,迅速打开管理员shell,删除原工程,新建工程,哎,问题还是没解决...

继续查,再看英文官网,哎呦,有不同,多了个Quick Start,赶快点进去


npm install -g create-react-native-app

create-react-native-app FirstRnPro

cd FirstRnPro

npm start

一条一条执行完之后,发现运行不到手机上,提示一个bundle的错误,打开工程看了一下,用这个命令创建的工程,竟然少了

Android和iOS的文件夹,这怎么能运行的了呢,大哭


转回折腾FirstRn,咦,有 npm start,是不是也有 yarn start呢,敲入,启动,打开APK,神奇发生了,index.js竟然

有资源在载入,心跳砰然加速,等到加载完成,手机上竟然有显示了,有内容显示了呀呀呀...


改下app.js,reload一下,还有,好神奇有没有...


艾玛,一脸懵逼呀,赶快新建SecondRn,ThirdRN,只用三条命令,一切顺利运行,这是为什么呢?

react-native init SecondRn
cd SecondRn
react-native run-android

不会白屏了,但是用网页访问,还是同样的错误,还是要继续深入研究才行呀,加油吧,骚年!

有没有大神知道,还请指点下,十分感谢哦!



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值