javascriptreactjsjwt

Can't import jwt-decode in JavaScript file in React project


I created a React app and I try to make a separate file for functions. I required 'jwt-decode' library like that:

const jwt_decode = require("jwt-decode");

After that I used it like that:

const verifyToken = (token) => { console.log(jwt_decode(token)); };

And exported like that:

module.exports = { verifyToken };

When I tried to use it in my react page, I saw this error in the console:

Uncaught TypeError: jwt_decode is not a function

What I did wrong?


Solution

  • import { jwtDecode } from "jwt-decode";
    
    const token = "eyJ0eXAiO.../// jwt token";
    const decoded = jwtDecode(token);
    

    it would help if you imported like these in the react-app for more info go through this official doc https://www.npmjs.com/package/jwt-decode#documentation