om.facebook.react.common.JavascriptException: Can‘t find variable: typeAnnotation

2022-07-25,,,,

总的日志如下:

2020-12-28 11:29:42.692 13591-13637/? E/ReactNativeJS: Application TestStartRNActivity has not been registered.
    
    Hint: This error often happens when you're running the packager (local dev server) from a wrong folder. For example you have multiple apps and the packager is still running for the app you were working on before.
    If this is the case, simply kill the old packager instance (e.g. close the packager terminal window) and start the packager in the correct app folder (e.g. cd into app folder and run 'npm start').
    
    This error can also happen due to a require() error during initialization or failure to call AppRegistry.registerComponent.
2020-12-28 11:29:42.705 13591-13638/? E/AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
    Process: cn.xcfamily.ypad, PID: 13591
    com.facebook.react.common.JavascriptException: Can't find variable: typeAnnotation, stack:
    <unknown>@1482:17089
    h@2:1670
    <unknown>@1481:426
    h@2:1670
    <unknown>@1480:340
    h@2:1670
    <unknown>@1473:6339
    h@2:1670
    <unknown>@1468:297
    h@2:1670
    <unknown>@1462:4640
    h@2:1670
    <unknown>@1461:134
    h@2:1670
    <unknown>@1407:327
    h@2:1670
    <unknown>@562:243
    h@2:1670
    <unknown>@408:455
    h@2:1670
    <unknown>@407:216
    h@2:1670
    <unknown>@367:237
    h@2:1670
    <unknown>@11:58
    h@2:1670
    d@2:868
    global code@1743:4

我是如何处理这个问题的,首先找到出错的地方;
在 @1482:17089
如果通过RN的红屏报错,我们就可以知道,这个地方是在index.android.bundle(1482:17089)
找到这个地方:
e.typeAnnotation=typeAnnotation
然后看一下,发现确实没有typeAnnotation的定义。
包着试试的心态,在RN中注入typeAnnotation,
发现确实存在
import {typeAnnotation} from “@babel/types/lib”;
运气很好,进入到import {typeAnnotation} from “@babel/types/lib”;
中发现:

exports.typeAlias = typeAlias;
exports.typeAnnotation = typeAnnotation;
exports.typeCastExpression = typeCastExpression;
exports.typeParameter = typeParameter;

对比了下index.android.bundle中的代码,发现,原来,其他的export都是通过方法返回的,唯独exports.typeAnnotation = typeAnnotation;没有返回方法,所以报错说未定义就能解释得通了。
顺带说一句,这个在手机上运行是没问题的,当我移植到Android平板上才报错。
所以我猜测原因可以是名字取成一样导致的不识别。
所以我的解决办法是,将
exports.typeAnnotation = typeAnnotation;改成
exports.typeAnnotation = typeAnnotationTo;
同时将

function typeAnnotation(typeAnnotation) {
return (0, _builder.default)(“TypeAnnotation”, …arguments);
}
改成

function typeAnnotationTo(typeAnnotation) {
return (0, _builder.default)(“TypeAnnotation”, …arguments);
}

再次bundle,
index.android.bundle中的结果就变成了
e.typeAnnotation=function(t){return o.default.apply(void 0,[“TypeAnnotation”].concat(Array.prototype.slice.call(arguments)))}
看到这里,其实就能想像,这个对的。
运行后,果然是没问题的。

本文地址:https://blog.csdn.net/mo_feng_/article/details/111842045

《om.facebook.react.common.JavascriptException: Can‘t find variable: typeAnnotation.doc》

下载本文的Word格式文档,以方便收藏与打印。