在做模拟登陆时,需要对数据进行解密。其中需要使用到jsencrypt库。写入代码

var JSEncrypt = require('jsencrypt')

报错

Error [ERR_REQUIRE_ESM]: require() of ES Module 
jsencrypt.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "mod
ule" which declares all .js files in that package scope as ES modules.
Instead rename jsencrypt.js to end in .cjs, change the requiring code to use dynamic import() which is available in all
CommonJS modules, or change "type": "module" to "type": "commonjs" 

于是我将加密库中的jsencrypt.js改为文件名改为jsencrypt.cjs,仍然报错:

ReferenceError: window is not defined

这时找到文件 “jsencrypt.cjs” 并且在首行添加

window = global;

则解决问题,程序可以正常运行。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐