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

总的日志如下:

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)))}
看到这里,其实就能想像,这个对的。
运行后,果然是没问题的。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值