React Native Expo应用调用Date构造函数报错:constructor is not callable
解决React Native Expo中new Date()报TypeError: constructor is not callable的问题
核心问题出在你安装的firebase-admin包上——这个是专门给Node.js后端使用的工具包,完全不能在React Native/Expo的前端环境中引入。它会污染全局运行环境,破坏原生的Date构造函数,才导致了这个报错。
解决步骤:
- 先卸载
firebase-admin:npm uninstall firebase-admin # 用yarn的话执行下面这句 yarn remove firebase-admin - 如果需要在前端使用Firebase功能,换成前端专用的
firebase包:
后续按照Firebase官方的前端SDK文档配置即可。npm install firebase # 用yarn的话执行下面这句 yarn add firebase
验证
卸载完成后重启Expo开发服务器,再运行你的代码,new Date()就能正常工作了——你的代码本身没有问题,原生Date在Expo和React Native中是完全支持的,就是这个后端包引发的环境冲突。
内容的提问来源于stack exchange,提问作者Mo Miah
相关产品推荐
相关产品推荐

