skip to Main Content
import jwt_decode from "jwt-decode"

 useEffect(() => {
        const token = localStorage.getItem("token")
        if(token){
            const user = jwt_decode(token)
        }
    }, [])

Error: export ‘default’ (imported as ‘jwt_decode’) was not found in ‘jwt-decode’ (possible exports: InvalidTokenError, jwtDecode)

I am trying to use the jsonwebtoken to decode the user information in React app and I took some references on the examples even I did npm i jwt-decode --save it still could not work properly as expected. May I ask what I am missing in order to solve the issue?

3

Answers


  1. I think the issue is the export has been updated in the jwt-decode package. The default export is no more exists in the package.

    The new export is jwtDecode. The error you have got also says about this.

    import { jwtDecode } from "jwt-decode";
    
     useEffect(() => {
            const token = localStorage.getItem("token");
            if(token){
                const user = jwtDecode(token);
            }
        }, [])
    

    Please refer to their official documentation for more information.

    Login or Signup to reply.
  2. Jwt-decode doesn’t have a default export, and jwt_decode isn’t a valid option.

    This is the correct import to use the function that you need.

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

    If you’d like you can set an alias changing jwtDecode to jwt_decode, like this:

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

    However I’d recommend using the function the way it’s served, it makes reading much easier

    Login or Signup to reply.
  3. According to the README it should be:

    import { jwtDecode } from "jwt-decode";
    

    So the JavaScript error message is telling the truth (as it nearly always does) 😉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search