当前位置:网站首页>vite项目require语法兼容问题解决require is not defined
vite项目require语法兼容问题解决require is not defined
2022-06-30 08:25:00 【天渺工作室】
vite项目不支持require语法问题解决 require is not defined
Vite默认使用es6标准的 import 的导入方式,不支持require引入。默认有Vite自己的引入方式https://vitejs.cn/guide/assets.html
但在在日常使用Vue3+Ts+Vite构建项目中,部分比较旧的三方插件中默认使用的是require引入(webpack构建可以使用),会出现require方法报错的兼容性问题 require is not defined 。
1. require和import导入区别
| 年份 | 出处 | 使用 | |
|---|---|---|---|
| require/exports | 2009 | CommonJS | const lodash = require(‘lodash’); |
| import/export | 2015 | ECMAScript2015(ES6) | import lodash from ‘lodash’; |
2. 解决方法
Vite 仓库社区生态 https://github.com/vitejs/awesome-vite中有一个
插件 vite-plugin-require-transform。 插件自动转换import xxx from ‘’;方法 引入
安装
yarn add -D vite-plugin-require-transform
或者
npm i vite-plugin-require-transform --save-dev
使用
// vite.config.ts配置
import requireTransform from 'vite-plugin-require-transform';
plugins: [
// ...
requireTransform({
fileRegex:/.ts$|.tsx$|.vue$/
// fileRegex:/.js$|.jsx$|.vue$/
}),
]
添加之后npm run build打包 npm run preview 预览无异常。
边栏推荐
- End-to-end 3D Point Cloud Instance Segmentation without Detection
- 涂鸦Wi-Fi&BLE SoC开发幻彩灯带
- [JUC series] overview of fork/join framework
- MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
- [untitled]
- 增强for循环的增删操作 & 迭代器删除集合元素
- PHP API to obtain QR code and combine to generate pictures
- Hit the industry directly | the flying propeller launched the industry's first model selection tool
- Niuke White Moon race 52
- Sword finger offer II 074 Merge interval (sort, array)
猜你喜欢

Environment configuration of ROS Aubo manipulator

Cesium learning notes (VI) particle system

【NVMe2.0b 14-5】Firmware Download/Commit command

Redis设计与实现(四)| 主从复制

Construction of energy conservation supervision system for campus buildings of ankery University

示波器探头对测量电容负荷有影响吗?

Game 280 problem2: minimum operands to turn an array into an alternating array

Cesium learning notes (III) creating instances
![[nvme2.0b 14-8] set features (Part 2)](/img/fe/67fd4f935237f9aa835e132e696b98.png)
[nvme2.0b 14-8] set features (Part 2)

Gilbert Strang's course notes on linear algebra - Lesson 4
随机推荐
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
Unity simple shader
Acreems energy efficiency management platform escorts the power safety of high-rise residential areas
codeforces每日5题(均1700)-第三天
自制GIF动态图-gifcam
Redis设计与实现(六)| 集群(分片)
TP5 set direct download file
【NVMe2.0b 14-4】Directive Send/Receive command
el-input 限制只能输数字
How to handle the expired data of redis and what are the elimination mechanisms?
微信公众号第三方平台开发,零基础入门。想学我教你啊
Redis设计与实现(四)| 主从复制
C# ListBox如何获取选中的内容(搜了很多无效的文章)
从0开始构建一个瀚高数据库Docker镜像
Cesium learning notes (III) creating instances
Cesium learning notes (I)
This point in JS
Opencv video
Deploy the cow like customer network project on the ECS
1162 Postfix Expression