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

Support node.js ? ReferenceError: window is not defined #297

Open
KingAmo opened this issue Jun 15, 2023 · 6 comments
Open

Support node.js ? ReferenceError: window is not defined #297

KingAmo opened this issue Jun 15, 2023 · 6 comments

Comments

@KingAmo
Copy link

KingAmo commented Jun 15, 2023

  • node v16.19.1
  • jsencrypt 3.3.2
ReferenceError: window is not defined
    at Object.<anonymous> (/Users/x/x/node_modules/jsencrypt/bin/jsencrypt.js:18:4)
    at Module._compile (node:internal/modules/cjs/loader:1191:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10)
    at Module.load (node:internal/modules/cjs/loader:1069:32)
    at Function.Module._load (node:internal/modules/cjs/loader:904:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
@zhumenglonge
Copy link

降为3.2.1;在首行加上global.window = this试试

@lanzhsh
Copy link

lanzhsh commented Jul 3, 2023

这种线上打包 还要手动更改node_modules 包, 太不友好了

@aliothor
Copy link

aliothor commented Jul 4, 2023

import JSEncrypt from 'jsencrypt/lib/index.js'
const crypt = new JSEncrypt.default()

在node中使用可以这样用

@airwin
Copy link

airwin commented Aug 4, 2023

降为3.2.1;在首行加上global.window = this试试

加 global.window = this 可用

@yantx
Copy link

yantx commented Nov 25, 2023

今天也遇到了这个问题mock模拟数据时使用了jsencrypt加密出现的版本3.3.2
引入方式:

 import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
 let decrypt = new JSEncrypt()

解决方式:在jsencrypt.min.js文件中添加如下代码

const window=this

@lancelot-song
Copy link

lancelot-song commented Apr 29, 2024

Because import jsencrypt before global.window not create,it will result load to failed global.window,so you should use js the single-threaded,let global.window is created before to import jsencrypt, try

(async () => {
  global.window = this;
  const JSEncrypt = await import('jsencrypt');
  const jsencrypt = new JSEncrypt.default();
  jsencrypt.setPublicKey(publicKey); // u public key
  console.log(`token:\r\n${jsencrypt.encrypt(RSA_VALUE)}`);
})();

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

7 participants