auth0 / node-jsonwebtoken

JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
MIT License
17.5k stars 1.21k forks source link

import `jsonwebtoken` throws error #963

Open jim-king-2000 opened 3 months ago

jim-king-2000 commented 3 months ago

Description

import jsonwebtoken throws error

Reproduction

The code:

import { verify } from "jsonwebtoken";

The error message:

import { verify } from "jsonwebtoken";
         ^^^^^^
SyntaxError: Named export 'verify' not found. The requested module 'jsonwebtoken' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'jsonwebtoken';
const { verify } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:120:12)

Node.js v21.7.0

It works on v9.0.0 but fails on 9.0.2.

Environment

Please provide the following:

amandesai01 commented 2 months ago

Facing same issue! what happened?

For me, not working with even 9.0.0. I can access other functions such as decode

fuadsaud commented 2 months ago

I started experiencing this problem after I switched to using ESM modules on my code. The solution, as the error message suggests, was to stop using named exports and rely on the default import syntax.

IYATT-yx commented 2 months ago

It can be written like this

import jwt from 'jsonwebtoken'; const { sign, verify } = jwt;

amandesai01 commented 2 months ago

I just commented @ts-ignore before calling verify method and it all started working. Weird. Can anyone tell me what might be the issue? Using Nitro server.