jsencrypt
jsencrypt copied to clipboard
ReferenceError: window is not defined when initializing a new class instance in AWS Lambda
Hi,
Really hoping to get this working. I am trying to use this in AWS lambda an am getting the following error in CloudWatch:
2021-09-16T18:42:33.067Z undefined ERROR Uncaught Exception {
"errorType": "ReferenceError",
"errorMessage": "window is not defined",
"stack": [
"ReferenceError: window is not defined",
" at Module.8078 (/var/task/simpleLogin.js:1:1310260)",
" at n (/var/task/simpleLogin.js:34:5318)",
" at /var/task/simpleLogin.js:34:5952",
" at /var/task/simpleLogin.js:34:9741",
" at Object.<anonymous> (/var/task/simpleLogin.js:34:9847)",
" at Module._compile (internal/modules/cjs/loader.js:1072:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
" at Module.load (internal/modules/cjs/loader.js:937:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
" at Module.require (internal/modules/cjs/loader.js:961:19)"
]
}
This is a scaled down version of the lambda
import { JSEncrypt } from 'jsencrypt';
export const SimpleLoginHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResultV2> => {
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
<PRIVATE KEY STRING>
-----END RSA PRIVATE KEY-----`;
console.log({PRIVATE_KEY});
const crypt = new JSEncrypt(); // THIS IS WHAT IS THROWING THE ERROR
console.log({crypt});
// crypt.setKey(PRIVATE_KEY);
// const password = crypt.decrypt(BODY.Password) as string;
// console.log({password});
// default response
return {
statusCode: 200,
headers: corsHeaders,
body: JSON.stringify({
Credentials: credentials,
Profile: profile
})
};
};
I was stuck with that error window is not defined
for quite a while too in my Next.js project, then eventually I try to dynamic load the package, and it works for me.
export const encrypt = async (message: string): Promise<string> => {
const JSEncrypt = (await import('jsencrypt')).default;
const jsencrypt = new JSEncrypt();
jsencrypt.setPublicKey('rsa_public_key');
return jsencrypt.encrypt(message) || message;
};
error "window" is not available during server side rendering.
9 | rng_pptr = 0; 10 | var t = void 0;
11 | if (window.crypto && window.crypto.getRandomValues) { | ^ 12 | // Extract entropy (2048 bits) from RNG if available 13 | var z = new Uint32Array(256); 14 | window.crypto.getRandomValues(z);
[0m [0m [0m[97m[41mWebpackError[0m[37m[41m:[0m[37m[41m [0m[97m[41mReferenceError: window is not defined[0m 佛了
I was stuck with that error
window is not defined
for quite a while too in my Next.js project, then eventually I try to dynamic load the package, and it works for me.export const encrypt = async (message: string): Promise<string> => { const JSEncrypt = (await import('jsencrypt')).default; const jsencrypt = new JSEncrypt(); jsencrypt.setPublicKey('rsa_public_key'); return jsencrypt.encrypt(message) || message; };
In my case this worked partially. It now did not trigger the error on loading the package but still when using it. In my case I needed to add globalThis.window = {} as unknown as any;
before using JSEncrypt in my TypeScript Azure Function.
I was stuck with that error
window is not defined
for quite a while too in my Next.js project, then eventually I try to dynamic load the package, and it works for me.export const encrypt = async (message: string): Promise<string> => { const JSEncrypt = (await import('jsencrypt')).default; const jsencrypt = new JSEncrypt(); jsencrypt.setPublicKey('rsa_public_key'); return jsencrypt.encrypt(message) || message; };
It works for me.