IoTSharp前端VUE采用npm run build编译提示require() of ES Module 出错

IoTSharp前端代码在IoTSharp/ClientApp是采用VUE开发的代码,采用npm run build编译不通过,出现如下问题:

一、问题1:Error [ERR_REQUIRE_ESM]: require() of ES Module

IoTSharp前端代码ClientApp里用npm run build编译提示如下错语"The CJS build of Vite's Node API is deprecated. See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. failed to load config from E:\IOT\IoTSharp\IoTSharp\ClientApp\vite.config.ts error during build: Error [ERR_REQUIRE_ESM]: require() of ES Module E:\IOT\IoTSharp\IoTSharp\ClientApp\node_modules\@iconify\utils\node_modules\package-manager-detector\dist\detect.mjs not supported. Instead change the require of E:\IOT\IoTSharp\IoTSharp\ClientApp\node_modules\@iconify\utils\node_modules\package-manager-detector\dist\detect.mjs to a dynamic import() which is available in all CommonJS modules.

这个错误表明在构建过程中,Vite 无法正确加载一个 ES Module(.mjs 文件),因为使用了 require(),而 ES Modules 不支持通过 require() 加载。

Vite 的 CJS 构建已被弃用,建议迁移到 ECMAScript Modules (ESM)。以下是两种解决方案:

1)解决方案:在 package.json 中添加 "type": "module"

在项目的 package.json 文件中添加 "type": "module",这会告诉 Node.js 将 .js 文件作为 ESM 处理:

复制代码
{
  "type": "module"
}

通过在 package.json 中添加 "type": "module" 或将 vite.config.ts 文件改名为 vite.config.mts,可以解决 Vite 的 CJS 构建弃用警告。通过动态导入 ES Modules、更新依赖项、清理和重新安装依赖、启用详细日志,通常可以解决 ERR_REQUIRE_ESM 错误。

二、问题2:vueSetupExtend is not a function

vite:config bundled config file loaded in 959.98ms +0ms failed to load config from E:\IOT\IoTSharp\IoTSharp\ClientApp\vite.config.ts error during build: TypeError: vueSetupExtend is not a function。

根据你的问题和提供的搜索结果,以下是解决 TypeError: vueSetupExtend is not a function 错误的详细步骤:

1)解决方案: 安装 vite-plugin-vue-setup-extend组件

错误信息表明 vueSetupExtend 被错误地导入或使用了。确保你正确导入了 vueSetupExtend 插件。根据搜索结果,vite-plugin-vue-setup-extend-plus 插件可能存在兼容性问题,建议使用 vite-plugin-vue-setup-extend

三、重新编译问题解决

相关推荐
VT.馒头26 分钟前
【力扣】2695. 包装数组
前端·javascript·算法·leetcode·职场和发展·typescript
css趣多多37 分钟前
一个UI内置组件el-scrollbar
前端·javascript·vue.js
-凌凌漆-1 小时前
【vue】pinia中的值使用 v-model绑定出现[object Object]
javascript·vue.js·ecmascript
C澒1 小时前
前端整洁架构(Clean Architecture)实战解析:从理论到 Todo 项目落地
前端·架构·系统架构·前端框架
C澒1 小时前
Remesh 框架详解:基于 CQRS 的前端领域驱动设计方案
前端·架构·前端框架·状态模式
Charlie_lll1 小时前
学习Three.js–雪花
前端·three.js
onebyte8bits1 小时前
前端国际化(i18n)体系设计与工程化落地
前端·国际化·i18n·工程化
C澒2 小时前
前端分层架构实战:DDD 与 Clean Architecture 在大型业务系统中的落地路径与项目实践
前端·架构·系统架构·前端框架
BestSongC2 小时前
行人摔倒检测系统 - 前端文档(1)
前端·人工智能·目标检测
0思必得02 小时前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化