auth0 / node-jsonwebtoken

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

Error using jsonwebtoken: global is not defined #962

Open DiegoZumarragaLS opened 4 months ago

DiegoZumarragaLS commented 4 months ago

Description

I'm just trying to create a jwt token using jsonwebtoken version 8.5.1. When I run my web application in a browser I get this error: Uncaught ReferenceError: global is not defined

The error is in this file: browser.js specifically on line 16 var crypto = global.crypto || global.msCrypto

Here are some prints: Error jsonwebtoken 1 Error jsonwebtoken 2

Reproduction

Here is the code I'm using

import * as jwt from 'jsonwebtoken';

export class TokenGenerator {
    generateToken(secret: string, user: string, scope: string): string {
        try {

            var secretKey = secret;

            const token = jwt.sign({ user, scope }, secretKey, { expiresIn: '1h' });

            return token;
        } catch (error) {
            // Handle the error here
            console.error('Error generating token:', error);
            throw error;
        }
    }
}

Environment

panva commented 4 months ago

Your issue is that you're transpiling/compiling with webpack with enabled polyfills.

DiegoZumarragaLS commented 4 months ago

Thanks for your response, any idea how to fix it?