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

React Native中jwt-decode报错_jwtDecode.default未定义的解决咨询

解决jwt-decode在React Native中出现_jwtDecode.default is not a function的问题

这个错误的核心原因是jwt-decode版本的导出方式不匹配:v3及以上版本采用命名导出,而你的代码使用了默认导入的写法,导致找不到对应函数。以下是两种可行的解决办法:

方法一:修改导入代码适配最新版本

如果你安装的是jwt-decode v3.x及以上版本,将导入语句从默认导入改为命名导入:

import { jwtDecode } from "jwt-decode";

export function hasExpiredToken(token) {
  const data = jwtDecode(token);
  console.log(data);
  return false;
}

修改后直接运行代码即可,无需额外更新或降级库。

方法二:降级到jwt-decode v2版本

如果你想保留原有导入写法,可以卸载当前版本,安装v2.x稳定版:

# 卸载当前版本
npm uninstall jwt-decode
# 安装兼容默认导入的v2.2.0版本
npm install jwt-decode@2.2.0

安装完成后,原有代码无需任何修改就能正常运行。

补充说明

你已经确认token本身无问题,因此只需根据需求选择上述任意一种方法,即可解决该类型错误。

内容的提问来源于stack exchange,提问作者Isaac Rodriguez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:12:01