travist / jsencrypt

A zero-dependency Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
http://www.travistidwell.com/jsencrypt
Other
6.65k stars 2.01k forks source link

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

Open KingAmo opened 1 year ago

KingAmo commented 1 year ago
zhumenglonge commented 1 year ago

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

lanzhsh commented 1 year ago

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

aliothor commented 1 year ago
import JSEncrypt from 'jsencrypt/lib/index.js'
const crypt = new JSEncrypt.default()

在node中使用可以这样用

airwin commented 1 year ago

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

加 global.window = this 可用

yantx commented 10 months ago

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

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

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

 const window=this
lancelot-song commented 5 months ago

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)}`);
})();