angularionic-frameworknpmangular2-servicescapacitor

"jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function" Error in ionic/angular/capacitor while trying to decode token with jwt-decode npm package


I have utilized jwt-decode package to decode the received token.

import * as jwt_decode from "jwt-decode";

and on receiving a valid token, let's call it variable tok.

On decoding the token

var tokenDecoded = jwt_decode(tok)

I'm getting the following error

Unhandled Promise rejection: jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function ; Zone: <root> ; Task: Promise.then ; Value: TypeError: jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function
at AuthService.<anonymous> (auth.service.ts:193)
at Generator.next (<anonymous>)
at fulfilled (tslib.es6.js:71)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Zone.run (zone-evergreen.js:124)
at zone-evergreen.js:855
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559) TypeError: jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function
at AuthService.<anonymous> (http://localhost:8100/main.js:1431:32)
at Generator.next (<anonymous>)
at fulfilled (http://localhost:8100/vendor.js:218136:58)
at ZoneDelegate.invoke (http://localhost:8100/polyfills.js:3397:26)
at Zone.run (http://localhost:8100/polyfills.js:3162:43)
at http://localhost:8100/polyfills.js:3893:36
at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3429:31)
at Zone.runTask (http://localhost:8100/polyfills.js:3206:47)
at drainMicroTaskQueue (http://localhost:8100/polyfills.js:3597:35)

Solution

  • I did this in my ionic/angular/capacitor app for decode the jwt token :

    npm install @types/jwt-decode

    Add the import :

    import jwt_decode from 'jwt-decode';

    And in your code (tok is your token to decode) :

    const tokenDecoded = jwt_decode(tok)