0

I need to use this lib in Ionic: https://github.com/auth0/node-jsonwebtoken It's to sign a token (yes, I need to sign a jwt in my front end)

It was working in Ionic 3! I was using the "declare var require: any;" trick from this post: Angular 4: "Cannot find name 'require'

All was perfect, good

But i am now in Ionic 4 and... Nothing! It's compile, ok, great, but I always had this error:

ERROR Error: "Uncaught (in promise): TypeError: obj is not an object or null

If a use a simple js file with the same code to sign a token (and run this file just with node), it's also work!

I only have this issue on Ionic 4. But not on previous version Ionic 3

If there are any recommendations on angular/ionic libraries to sign a token (instead of jsonwebtoken), that would be helpful.

pontusv
  • 283
  • 1
  • 13
Caribou
  • 39
  • 1
  • 5

1 Answers1

2

Try to install types for the jsonwebtoken library: npm i @types/jsonwebtoken

And then import it with the ES6 style: import jwt from 'jsonwebtoken'; instead of require...

igor_c
  • 1,200
  • 1
  • 8
  • 20
  • and I still must have the original jsonwebtoken, right? I have tested, still doesn't work "TypeError: obj is not an object or null" with import * as jwt from 'jsonwebtoken'; (yes because jsonwebtoken haven't default export) what must I have in my tsconfig? module: es6? types: ["nodes"] ? – Caribou Oct 06 '19 at 08:45
  • I also have tried with another @types/... the @types/jws And always the same error – Caribou Oct 06 '19 at 08:57
  • @Caribou here I tried it out https://stackblitz.com/edit/ionic-qkrrfy and it looks like it works. All I did just installed `jsonwebtoken` and `@types/jsonwebtoken` as I wrote in the answer and imported it exactly like in the answer. If you open the console, you'll see the `jwt` object with it's functions and stuff. Maybe your error is somewhere else? – igor_c Oct 06 '19 at 09:28
  • the difference is typescript v2 (I have v3 I can't past to v2 for angular 7) or maybe with the tsconfig file I will continue to search – Caribou Oct 06 '19 at 09:59