记录: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",

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

相关推荐
祈澈菇凉1 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w2 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
记得早睡~2 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
庸俗今天不摸鱼3 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
奔跑吧邓邓子3 小时前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程
熬夜不洗澡3 小时前
Node.js中不支持require和import两种导入模块的混用
node.js
bubusa~>_<3 小时前
解决npm install 出现error,比如:ERR_SSL_CIPHER_OPERATION_FAILED
前端·npm·node.js
[廾匸]4 小时前
cesium视频投影
javascript·无人机·cesium·cesium.js·视频投影
菲力蒲LY4 小时前
vue 手写分页
前端·javascript·vue.js
一丢丢@zml4 小时前
new 一个构造函数的过程以及手写 new
javascript·手写new