node-jsonwebtoken: can't import jsonwebtoken in modern typescript esm app

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"
    

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 35
  • Comments: 18

Commits related to this issue

Most upvoted comments

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

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

and the types all come through fine

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.

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.

@jd-0001 that is not related to this issue

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

This worked for me!

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

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'

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

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.

@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

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

Just use “fastify/jwt” or “jwt-decode” pacakge

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

Thanks.