Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

can't import jsonwebtoken in modern typescript esm app

Aperta
#785 18 commenti 37 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
node.js, typescript

Direzione di ricerca

Non vengono indicati file o test. Inizia dagli esempi di importazione e dalla issue correlata #655, quindi esamina le esportazioni dei moduli attuali del package e la configurazione delle dichiarazioni. Il lavoro è completato quando un’app ESM moderna in TypeScript può importare sign, verify e i relativi tipi delle opzioni senza uno shim, e il comportamento a runtime e i typings concordano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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"
    
Lingua principale
JavaScript
Stelle
18.2k
Fork
1.3k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di auth0/node-jsonwebtoken

Tutte le issue di auth0/node-jsonwebtoken

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.