auth0 / node-jsonwebtoken

JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
MIT License
17.74k stars 1.23k forks source link

can't import jsonwebtoken in modern typescript esm app #785

Open chase-moskal opened 3 years ago

chase-moskal commented 3 years ago

we can't properly import this library in a modern typescript project. here's the problem:

as you can see there's an incompatibility between what can actually be imported at runtime, versus what the @types typings allow us to import. on one hand, this could be seen as a deficiency in the community @types typings

on the other hand, a fundamental fix for this problem would be:

related issue #655


in the meantime, here's the hack i used to circumvent this problem

  1. create a shim module badmodules/jsonwebtoken.ts

    import * as _jsonwebtoken from "jsonwebtoken"
    const jsonwebtoken = <any>_jsonwebtoken
    
    import {
      sign as signType,
      verify as verifyType,
      SignOptions,
      VerifyOptions,
      Algorithm,
    } from "jsonwebtoken"
    
    const sign: typeof signType = jsonwebtoken.default.sign
    const verify: typeof verifyType = jsonwebtoken.default.verify
    
    export {
      sign,
      verify,
      Algorithm,
      SignOptions,
      VerifyOptions,
    }
  2. import the shim module instead

    import {sign, verify, SignOptions, VerifyOptions, Algorithm}
      from "./badmodules/jsonwebtoken.js"
diroussel commented 2 years ago

This works for me in typescript... import { Secret, decode, verify } from 'jsonwebtoken';

and the types all come through fine

jd-solanki commented 2 years ago

I am using vite + vue 3 and getting below error: Screenshot 2021-12-15 at 3 51 51 PM

diroussel commented 2 years ago

@jd-0001 that is not related to this issue

jd-solanki commented 2 years ago

@diroussel Sorry, I thought this was related to this. May I know what's causing this so I can open new issue here.

Thanks.

diroussel commented 2 years ago

I would suggest setting a debugger breakpoint and following what is really going on. If you are able to get a working code sample maybe post it on stackoverflow. But github issues are to report bugs or request features, not to ask for debugging help.

stipsan commented 2 years ago

Hi! I had some ESM troubles and made this package: stipsan/jsonwebtoken-esm While at it I fixed some of the TS issues, and added support for typing on stuff like: import decode from 'jsonwebtoken-esm/decode'. The lib isn't intended to live forever, as I assume auth0 will at some point publish an ESM version, so you can use this to check what version of jsonwebtoken is under the hood: import {version} from 'jsonwebtoken-esm'

vekunz commented 2 years ago

Unfortunately jsonwebtoken-esm does not work in node scripts (and it seems that it is not intended to work in node scripts).

We now use jsonwebtoken again and import it this way:

import jsonwebtoken from 'jsonwebtoken';
const { sign, decode, verify } = jsonwebtoken;

This works fine for SvelteKit + Vite.

But nevertheless it would be great if this package can get native ESM support.

mikenikles commented 2 years ago

Hi @vekunz, I found your comment and have the exact same code for SvelteKit + Vite. The web app is deployed to Vercel and I notice I'm now getting the following error at runtime:

2022-10-16T22:19:35.390Z    aa6e5ab6-a5ee-45f6-b8a6-6a684842da7d    ERROR   TypeError: signJwt is not a function
    at file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/entries/endpoints/login/github/callback/_server.ts.js:235:5
    at new Promise (<anonymous>)
    at signJwtAndSerializeCookie (file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/entries/endpoints/login/github/callback/_server.ts.js:234:10)
    at GET (file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/entries/endpoints/login/github/callback/_server.ts.js:276:30)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async render_endpoint (file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/index.js:609:22)
    at async resolve (file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/index.js:2511:22)
    at async handleUser (file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/chunks/hooks.server.js:47:20)
    at async respond (file:///var/task/vercel/path0/services/website/.svelte-kit/output/server/index.js:2551:22)
    at async Server.default (file:///var/task/vercel/path0/services/website/.svelte-kit/vercel-tmp/index.js:31:3)

This is with Node.js 16 configured on Vercel. If you host on Vercel, do you use Node.js 16? If not on Vercel, do you mind sharing where your app is hosted?

Thanks

vekunz commented 2 years ago

Hello @mikenikles, we use SvelteKit with adapter-node and run it in a Docker container with Node 16.

BasilaryGroup commented 1 year ago

There is a very simple work around, use https://github.com/panva/jose

It look less than 2 minutes to install and modify my code.

svedova commented 1 year ago

There is a very simple work around, use https://github.com/panva/jose

It look less than 2 minutes to install and modify my code.

I ended up doing the same. Locally it works fine while developing but I realized this is broken when I built on Stormkit. Tried building locally, it gives me the same error:

Error: Failed to resolve entry for package "ms". The package may have incorrect main/module/exports specified in its package.json.

It works fine with https://github.com/panva/jose though, took a couple of minutes to rewrite as mentioned! Thanks 🙏

behkha commented 9 months ago

I am using vite + vue 3 and getting below error: Screenshot 2021-12-15 at 3 51 51 PM

@jd-solanki Did you find any solution for this error?

jd-solanki commented 8 months ago

No 😞

jaq-czaplin commented 7 months ago

I was able to properly load jsonwebtoken with import { default as jwt } from 'jsonwebtoken'; on ESNext

se040054 commented 7 months ago

Almost all "import" methods only support older versions of Node.js.

Just use "fastify/jwt" or "jwt-decode" pacakge

RaghavRagh commented 6 months ago

can anyone fix this? Screenshot 2024-05-03 202650

ash-lionell commented 6 months ago

I was able to properly load jsonwebtoken with import { default as jwt } from 'jsonwebtoken'; on ESNext

This worked for me!

NomanQureshi1997 commented 4 months ago

@RaghavRagh you are trying to access the jwtwebtoken on client side which is not allowed, by implementing it on server side you will not be facing this issue