Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jwt-decode "This expression is not callable." with typscript

Just started seeing this error.

I know this is related to the question here: Using jwt-decode with typescript front-end project

However, I already am trying the answer and it’s not working…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

The Code

import jwt from 'jwt-decode';

export function isTokenExpired(token: string): boolean {
  // check token payload
  const tokenPayload: any = jwt(token);
  // check token expiration date in payload
  const tokenExpiration = tokenPayload.exp;
  const now = Math.floor(new Date().getTime() / 1000); // token exp shaved off milliseconds
  // if (expiration date is greater than current date)
  return now > tokenExpiration;
}

The Error

This expression is not callable.
  Type 'typeof import("c:/Projects/msal-react-native-expo/node_modules/jwt-decode/build/cjs/index")' has no call signatures.ts(2349)`

Note: I’m using this same code snippet in a react project and it’s working fine. However, this seems to be breaking in my react-native/expo project. Not sure if it’s a dom related issue or what the deal is. Is there an alternative solution?

>Solution :

https://github.com/auth0/jwt-decode/blob/main/CHANGELOG.md#migration-to-v400

Migration to v4.0.0

The jwtDecode function is now no longer the default export, and is instead provided as a named export. Make sure to update your code in places where you are importing this function:

-import jwtDecode from "jwt-decode";
+import { jwtDecode } from "jwt-decode";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading