can't import jsonwebtoken in modern typescript esm app

See original GitHub issue

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"
    

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:9

github_iconTop GitHub Comments

25reactions
jd-solankicommented, Dec 15, 2021

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

2reactions
vekunzcommented, Oct 17, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6 imports for JWT - javascript - Stack Overflow
5 Answers 5 · Create a folder · Do npm init · Create a file app.js · install json web token npm i...
Read more >
@types/jsonwebtoken - npm
This package contains type definitions for jsonwebtoken (https://github.com/auth0/node-jsonwebtoken). Details. Files were exported from https:// ...
Read more >
can't import the named export from default-exporting module ...
I'm having an issue importing this in my TypeScript React project. import { SwapWidget } from '@uniswap/widgets' ...
Read more >
Firebase JavaScript SDK Release Notes - Google
js and TypeScript. App Check. Fixed a bug where @firebase/app-check-types was not included ...
Read more >
How to fix error TS7016: Could not find a declaration file for ...
If you're using TypeScript you might have run into this error before. The second part of the error says: Try `npm install @types/XYZ`...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found