【问题标题】:Debugging react-native on android - com.facebook.react.devsupport.JSException: Unexpected token '*'在 android 上调试 react-native - com.facebook.react.devsupport.JSException: Unexpected token '*'
【发布时间】:2018-03-31 19:15:44
【问题描述】:

所以,首先...在 iOS 上运行没有问题。我在

“反应”:“16.0.0-beta.5”,

"react-native": "^0.49.3",

在物理设备或模拟器上尝试 react-native run-android 时出现以下错误。

我还尝试删除和重新安装节点模块,清除 npm 缓存,清理 android 构建。这些东西都不能解决这个问题。

如果我转到错误中的 url,则有一个 JS 页面。该应用程序还会锁定设备或模拟器。

其他信息:

adb 是我的路径,我可以运行 adb devices。

adb reverse tcp:8081 tcp:8081 没有帮助

10-19 12:28:20.839: E/unknown:ReactNative(12782): Exception in native call from JS
10-19 12:28:20.839: E/unknown:ReactNative(12782): com.facebook.react.devsupport.JSException: Unexpected token '*' (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:112538)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at android.os.Handler.handleCallback(Handler.java:751)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at android.os.Handler.dispatchMessage(Handler.java:95)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at android.os.Looper.loop(Looper.java:154)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   at java.lang.Thread.run(Thread.java:761)
10-19 12:28:20.839: E/unknown:ReactNative(12782): Caused by: com.facebook.jni.CppException: Unexpected token '*' (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:112538)
10-19 12:28:20.839: E/unknown:ReactNative(12782):   ... 7 more
10-19 12:28:20.841: E/unknown:ReactNative(12782): Exception in native call
10-19 12:28:20.841: E/unknown:ReactNative(12782): java.lang.RuntimeException: Error calling AppRegistry.runApplication
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at android.os.Handler.handleCallback(Handler.java:751)
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at android.os.Handler.dispatchMessage(Handler.java:95)
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at android.os.Looper.loop(Looper.java:154)
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
10-19 12:28:20.841: E/unknown:ReactNative(12782):   at java.lang.Thread.run(Thread.java:761)
10-19 12:28:20.841: E/unknown:ReactNative(12782): Caused by: com.facebook.jni.CppException: Could not get BatchedBridge, make sure your bundle is packaged correctly
10-19 12:28:20.841: E/unknown:ReactNative(12782):   ... 7 more

【问题讨论】:

    标签: react-native react-native-android


    【解决方案1】:

    所以经过多次调试,我找到了这个问题的原因,我会在这里发布,以防其他人遇到它。

    我的项目正在使用

    指数运算符**

    (我认为这是在 ES6 中引入的)。

    正如我在问题中所说,这在 iOS 上运行时不会导致任何问题。我还没有深入挖掘,但似乎 Android 方面的 react-native 代码库中肯定存在错误。也许是在圣经?我不是 100% 确定。

    我所知道的是

    例如从 2 ** 3 更改为 Math.pow(2,3) 可以解决此问题 我。

    更新这个答案:

    我无法重现此创建全新的 RN 项目。我遇到这个问题的项目是从RN 42升级的,所以升级过程肯定有问题。

    【讨论】:

      【解决方案2】:

      更新: babel-preset-react-native (v5.0.0) 现在支持指数运算符。预设的package.json 需要plugin-transform-exponentiation-operator


      babel-preset-react-native 支持指数运算符只是时间问题,但现在,您可以手动将babel-plugin-transform-exponentiation-operator 添加到您的项目中。

      yarn add babel-plugin-transform-exponentiation-operator --dev

      然后在您的.babelrc 文件中,确保您的plugins 属性与此类似:

      {
        "plugins": ["transform-exponentiation-operator"]
      }
      

      【讨论】:

        【解决方案3】:

        如果还有人遇到这个错误,试试这个

        ** operator breaks packager

        此解决方案为我解决了问题,但我不确定是什么导致了我的错误。 我的反应版本是

        “反应”:“16.0.0”

        “反应原生”:“0.50.4”

        总之,

        npm install babel-preset-react-native-stage-0
        

        然后将下面这一行添加到.babelrc

        {
          "presets": ["react-native-stage-0"]
        }
        

        如果 react native 已经存在,它就会存在

        {
          "presets": ["react-native", "react-native-stage-0"]
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-09
          • 1970-01-01
          • 1970-01-01
          • 2017-09-16
          • 1970-01-01
          • 1970-01-01
          • 2020-08-15
          • 1970-01-01
          相关资源
          最近更新 更多