当前位置: 首页 > news >正文

【Exception】Error: Dynamic require of “path“ is not supported

Talk is cheap, show me the code.

环境 | Environment

kversion
OSwindows 11
Node.jsv18.14.2
npm9.5.0
vite5.0.0
vue3.3.8

报错日志 | Error log

>npm run dev> app1@0.0.0 dev
> viteERROR  failed to load config from C:\code\frontend\app1\vite.config.js                22:01:42  ERROR  error when starting dev server:                                                                                    22:01:42  
Error: Dynamic require of "path" is not supportedat file:///C:/code/frontend/app1/vite.config.js.timestamp-1700661702409-1225e92edb929.mjs:6:9    at file:///C:/code/frontend/app1/vite.config.js.timestamp-1700661702409-1225e92edb929.mjs:14:12at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

vite.config.js

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createHtmlPlugin } from 'vite-plugin-html'const path = require('path')// 为了在index.html中使用环境变量
const getViteEnv = (mode, target) => {return loadEnv(mode, process.cwd())[target];
};// https://vitejs.dev/config/
export default ({ mode }) =>defineConfig({plugins: [vue(),createHtmlPlugin({inject: {data: {//将环境变量 VITE_APP_TITLE 赋值给 title 方便 html页面使用 title 获取系统标题title: getViteEnv(mode, "VITE_APP_TITLE"),},},}),],resolve: {alias: {'@': path.resolve(__dirname, "./src")}},})

原因分析 | Analysis

  1. VITE v5.0.0不支持require()

解决方案 | Solution

const path = require('path')改为import path from 'path'

参考 | References

简书 - vite项目中出现的错误Error: Dynamic require of “path” is not supported

http://www.lryc.cn/news/239603.html

相关文章:

  • 【蓝桥杯选拔赛真题25】C++两个数比大小 第十三届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
  • C++学习——C++运算符重载(含义、格式、示例、遵循的规则)
  • 【unity实战】unity3D中的PRG库存系统和换装系统(附项目源码)
  • 编程语言发展史:C语言的诞生及其影响
  • (二)pytest自动化测试框架之添加测试用例步骤(@allure.step())
  • 【用unity实现100个游戏之16】Unity程序化生成随机2D地牢游戏2(附项目源码)
  • 潮玩宇宙大逃杀游戏开发源码说明
  • UE5 操作WebSocket
  • Linux文件
  • 素短语的定义
  • 【华为OD题库-033】经典屏保-java
  • clang+llvm多进程gdb调试
  • PHP反序列化简单使用
  • 专业课140+总分420+东南大学920专业综合考研,信息学院通信专业考研分享
  • 数据结构与算法编程题11
  • 【LeetCode刷题】--40.组合总和II
  • mysql面试内容点
  • msvcp140.dll是什么?msvcp140.dll丢失的有哪些解决方法
  • 数字图像处理(冈萨雷斯)学习笔记
  • MES系统管理范围及标准
  • vscode运行dlv报错超时
  • 【Leetcode合集】1. 两数之和
  • 使用Java解决快手滑块验证码
  • 瑞吉外卖Day06
  • 从暗黑3D火炬之光技能系统说到-Laya非入门教学一~资源管理
  • for,while,until语句
  • Apache POI简介
  • 基于Qt的UDP通信、TCP文件传输程序的设计与实现——QQ聊天群聊
  • 【C++】:STL中的string类的增删查改的底层模拟实现
  • 【论文阅读笔记】Supervised Contrastive Learning