You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中用aws-amplify实现登录遇Logger构造函数错误求助

解决React Native中aws-amplify登录功能的Logger is not a constructor错误

错误原因

这个错误源于aws-amplify v6的API重大重构和UI库版本不兼容:

  1. aws-amplify v6彻底移除了Logger构造函数,改用全新的日志实现逻辑;
  2. 你安装的aws-amplify-react-native v6是官方废弃的旧包,内部仍在调用v5版本的Logger API,与v6核心库产生冲突;
  3. 官方在v6版本后已将React Native的UI组件迁移至@aws-amplify/ui-react-native,旧包不再维护更新。

解决方案

1. 卸载废弃的UI包

执行命令移除旧的aws-amplify-react-native:

npm uninstall aws-amplify-react-native
# 若使用yarn则执行:
yarn remove aws-amplify-react-native

2. 修改App.js代码,使用官方新UI库

替换withAuthenticator的导入源,并确保正确包裹根组件:

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Amplify } from 'aws-amplify';
// 从官方新UI库导入withAuthenticator
import { withAuthenticator } from '@aws-amplify/ui-react-native';
import { config } from './aws-exports';

Amplify.configure(config);

function App() {
  return (
    <View style={styles.container}>
      <Text>Open up App.js to start working on your app!</Text>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

// 用withAuthenticator包裹根组件
export default withAuthenticator(App);

3. 清理缓存并重启项目(可选但推荐)

避免缓存残留导致的异常,执行以下操作:

# 删除依赖包和锁文件
rm -rf node_modules package-lock.json yarn.lock
# 重新安装依赖
npm install
# 或 yarn install

# 清除Expo缓存并重启开发服务器
expo start --clear

验证

重新启动项目后,登录界面应能正常加载,Logger is not a constructor的错误将不再出现。

内容的提问来源于stack exchange,提问作者Shauij Msw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:22:09