Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chase-moskal opened this issue Jul 22, 2021 · 18 comments
Open

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

chase-moskal opened this issue Jul 22, 2021 · 18 comments

Comments

@chase-moskal
Copy link

chase-moskal commented Jul 22, 2021

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

  • import * as jwt from "jsonwebtoken"
    • export default contains what i need, sign, verify, etc
    • however, the types won't let me have it
  • @types/jsonwebtoken
    • does not have any definition for a default export

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:

  • upgrade jsonwebtoken for first-class esm support
  • maintain official typings

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
Copy link

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

and the types all come through fine

@jd-solanki
Copy link

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

@diroussel
Copy link

@jd-0001 that is not related to this issue

@jd-solanki
Copy link

@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
Copy link

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
Copy link

stipsan commented Apr 27, 2022

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
Copy link

vekunz commented Aug 23, 2022

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
Copy link

mikenikles commented Oct 16, 2022

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
Copy link

vekunz commented Oct 17, 2022

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

@BasilaryGroup
Copy link

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
Copy link

svedova commented Jan 22, 2023

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 🙏

alex9smith added a commit to govuk-one-login/account-management-stubs that referenced this issue Jul 25, 2023
DougsCER pushed a commit to govuk-one-login/account-management-stubs that referenced this issue Jul 25, 2023
DougsCER pushed a commit to govuk-one-login/account-management-stubs that referenced this issue Jul 25, 2023
divyaparameswaran pushed a commit to govuk-one-login/account-management-stubs that referenced this issue Jul 25, 2023
@behkha
Copy link

behkha commented Mar 2, 2024

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
Copy link

No 😞

@jaq-czaplin
Copy link

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

@se040054
Copy link

se040054 commented May 2, 2024

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

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

@RaghavRagh
Copy link

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

@ash-lionell
Copy link

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

This worked for me!

@NomanQureshi1997
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

15 participants