记录:Error [ERR_REQUIRE_ESM]: require() of ES Module not supported.报错

记录:在js中导入jsencrypt模块时,出现Error [ERR_REQUIRE_ESM]: require() of ES Module xxxxx from xxxxx not supported.报错

代码:

javascript 复制代码
const JSEncrypt = require("jsencrypt")

报错:

javascript 复制代码
const JSEncrypt = require("jsencrypt")
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\yj_pj\node_modules\jsencrypt\bin\jsencrypt.js from D:\yj_pj\YWF\test123\wenshu.js not supported.
jsencrypt.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" 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" in D:\yj_pj\no
de_modules\jsencrypt\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (D:\yj_pj\YWF\test123\wenshu.js:3:19) {
  code: 'ERR_REQUIRE_ESM'
}

解决办法:

将提示语翻译过来:

将jsencrypt.js重命名为以.cjs结尾,将所需代码更改为使用所有CommonJS模块中可用的dynamicimport(),或将D:\yj_pj\no中的"type":"module"更改为"type""CommonJS"

de_modules\jsencrypt\package.json将所有.js文件视为CommonJS(将.mjs用于所有ES模块)。

  1. 按照提示语将jsencrypt.js 重命名为 jsencrypt.cjs
  1. 将上一级目录中的package.json 下的 main 值同步修改为刚刚重命名的文件,

即"main": "bin/jsencrypt.js", --->> "main": "bin/jsencrypt.cjs",

修改好后再次回到我们写的文件中运行发现,可以正常运行了

相关推荐
漫路在线16 分钟前
JS逆向-某易云音乐下载器
开发语言·javascript·爬虫·python
程序员拂雨1 小时前
Node.js中的URL模块
node.js
BillKu2 小时前
Vue3 Element Plus 对话框加载实现
javascript·vue.js·elementui
初遇你时动了情3 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
前端小崔3 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
运维@小兵3 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js
小生凡一4 小时前
搜索引擎工作原理|倒排索引|query改写|CTR点击率预估|爬虫
爬虫·搜索引擎
CodeJourney.4 小时前
基于MATLAB的生物量数据拟合模型研究
人工智能·爬虫·算法·matlab·信息可视化
一只专注api接口开发的技术猿4 小时前
企业级电商数据对接:1688 商品详情 API 接口开发与优化实践
大数据·前端·爬虫
GISer_Jing4 小时前
[前端高频]数组转树、数组扁平化、深拷贝、JSON.stringify&JSON.parse等手撕
前端·javascript·json