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
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.Please refer to their official documentation for more information.
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.
If you’d like you can set an alias changing jwtDecode to jwt_decode, like this:
However I’d recommend using the function the way it’s served, it makes reading much easier
According to the README it should be:
So the JavaScript error message is telling the truth (as it nearly always does) 😉