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
defaultcontains what i need, sign, verify, etc - however, the types won’t let me have it
- export
@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
-
create a shim module
badmodules/jsonwebtoken.tsimport * 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, } -
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
- only import `sign` See https://github.com/auth0/node-jsonwebtoken/issues/785#issuecomment-988741883 — committed to govuk-one-login/account-management-stubs by alex9smith 3 years ago
- only import `sign` See https://github.com/auth0/node-jsonwebtoken/issues/785#issuecomment-988741883 — committed to govuk-one-login/account-management-stubs by alex9smith 3 years ago
- only import `sign` See https://github.com/auth0/node-jsonwebtoken/issues/785#issuecomment-988741883 — committed to govuk-one-login/account-management-stubs by alex9smith 3 years ago
- only import `sign` See https://github.com/auth0/node-jsonwebtoken/issues/785#issuecomment-988741883 — committed to govuk-one-login/account-management-stubs by alex9smith 3 years ago
I am using vite + vue 3 and getting below error:
This works for me in typescript…
import { Secret, decode, verify } from 'jsonwebtoken';and the types all come through fine
Unfortunately
jsonwebtoken-esmdoes not work in node scripts (and it seems that it is not intended to work in node scripts).We now use
jsonwebtokenagain and import it this way: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
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 ofjsonwebtokenis under the hood:import {version} from 'jsonwebtoken-esm'I was able to properly load jsonwebtoken with
import { default as jwt } from 'jsonwebtoken';on ESNextI 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.