can't import jsonwebtoken in modern typescript esm app
See original GitHub issuewe 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"
Issue Analytics
- State:
- Created 2 years ago
- Reactions:13
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am using vite + vue 3 and getting below error:
Hello @mikenikles, we use SvelteKit with adapter-node and run it in a Docker container with Node 16.