"ReferenceError: KJUR is not defined" JWT generation script

when I try to generate my JWT - I get a :ReferenceError: KJUR is not defined

this is the part of my pre-req script

function generateJwt() {
	eval(pm.globals.get('jsrsasign-js')); // import javascript jsrsasign

	var sHeader = JSON.stringify(header);
	var sPayload = JSON.stringify(payload);

	var JWT = KJUR.jws.JWS.sign(header.alg, sHeader, sPayload, jwtPrivateKey);

	pm.environment.set('assertionToken', signedToken);
	console.log('jwt', signedToken);
}

var navigator = {}; // fake a navigator object for the lib
var window = {}; // fake a window object for the lib

if (pm.globals.has('jsrsasign-js')) generateJwt();
else pm.sendRequest(
	'https://kjur.github.io/jsrsasign/jsrsasign-all-min.js',
	function (err, res) {
		if (err) {
			console.log(err);
		} else {
			pm.globals.set('jsrsasign-js', res.text());
			generateJwt();
		}});

any suggestion?

The error seems correct.

The only mention of the KJUR variable is on this line.

var JWT = KJUR.jws.JWS.sign(header.alg, sHeader, sPayload, jwtPrivateKey);

Where is the variable defined? For that matter, where are the other variables defined. header, payload and privatekey for example.

and normally KJUR was not defined at all, and suddenly out of nowhere, I get this message

this is the full script

var jwtPrivateKey = `-----BEGIN PRIVATE KEY-----

-----END PRIVATE KEY-----`;

// Set headers for JWT
var header = {
	'alg': 'RS256',
	'typ': 'JWT'
};

// Prepare timestamp in seconds
var currentTimestamp = Math.floor(Date.now() / 1000);

var payload = {
    'iss': 'XXX',
    'aud': 'XXX',
    'iat': currentTimestamp,
	'exp': currentTimestamp + 60 * 5,
    'jti': 'XXX',
    'sub': 'XXX',
    'consumer_id': 'XXX',
    'channel': 'XXX',
    'scope': 'XXX',
    'amr': []
}

function generateJwt() {
	eval(pm.globals.get('jsrsasign-js')); // import javascript jsrsasign

	var sHeader = JSON.stringify(header);
	var sPayload = JSON.stringify(payload);

	var JWT = KJUR.jws.JWS.sign(header.alg, sHeader, sPayload, jwtPrivateKey);

	pm.environment.set('assertionToken', signedToken);
	console.log('jwt', signedToken);
}

var navigator = {}; // fake a navigator object for the lib
var window = {}; // fake a window object for the lib

if (pm.globals.has('jsrsasign-js')) generateJwt();
else pm.sendRequest(
	'https://kjur.github.io/jsrsasign/jsrsasign-all-min.js',
	function (err, res) {
		if (err) {
			console.log(err);
		} else {
			pm.globals.set('jsrsasign-js', res.text());
			generateJwt();
		}});

Why can’t you use Crypto.JS which is available in the Postman sandbox?

Here’s a public collection with examples.

Generate JWT | Postman Team Collections | Postman API Network

It includes an example using the Authorization helper which you might want to consider.