Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using jsonwebtoken: global is not defined #962

Open
DiegoZumarragaLS opened this issue Feb 28, 2024 · 2 comments
Open

Error using jsonwebtoken: global is not defined #962

DiegoZumarragaLS opened this issue Feb 28, 2024 · 2 comments

Comments

@DiegoZumarragaLS
Copy link

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

  • Version of this library used: 8.5.1

  • Version of the platform or framework used, if applicable:
    NodeJS 20.11.1,
    typescript 4.9.3

  • Other relevant versions (language, server software, OS, browser):
    powerbi-visuals-tools 5.3.0

  • Other modules/plugins/libraries that might be involved:
    I'm not using Angular or React nor any other web framework

@panva
Copy link
Contributor

panva commented Feb 29, 2024

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

@DiegoZumarragaLS
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants