当前位置:网站首页>vite項目require語法兼容問題解决require is not defined
vite項目require語法兼容問題解决require is not defined
2022-06-30 08:27: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 預覽無异常。
边栏推荐
- Redis设计与实现(二)| 数据库(删除策略&过期淘汰策略)
- Applet uses QR code plug-in
- Experiment 4 QT
- Getordefault method of map class
- An example of a single service in a cloud project driven by a domain
- layer. Open processing method when the passed value is an array or the value is too long
- Map,String,Json之間轉換
- Swagger use
- 【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
- MySQL quotation sentence is unlocked: algorithm=insert, lock=none
猜你喜欢

Development technology sharing of Jingtan NFT digital collection system

What management improvements can CRM bring to enterprises
![[nvme2.0b 14-7] set features (Part 1)](/img/0d/c26ae2475ae69291d83b4096ea942b.png)
[nvme2.0b 14-7] set features (Part 1)

【NVMe2.0b 14-2】Create/Delete Queue

2021-05-06

Using typera+picgo to realize automatic uploading of markdown document pictures

Redis设计与实现(一)| 数据结构 & 对象

Redis设计与实现(七)| 发布 & 订阅

2021-04-29

【NVMe2.0b 14-5】Firmware Download/Commit command
随机推荐
Unit Test
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
[JUC series] overview of fork/join framework
This point in JS
Niuke Xiaobai month race 52
跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过
文件上传 upload 组件 on-success 事件,添加自定义参数
小心transmittable-thread-local的这个坑
Development technology sharing of Jingtan NFT digital collection system
[nvme2.0b 14-7] set features (Part 1)
Redis设计与实现(七)| 发布 & 订阅
【kotlin 协程】万字协程 一篇完成kotlin 协程进阶
Wsl2 using GPU for deep learning
Redis design and Implementation (V) | sentinel sentry
Swagger use
Redis design and Implementation (IV) | master-slave replication
Redis design and Implementation (VIII) | transaction
自制GIF动态图-gifcam
【NVMe2.0b 14-5】Firmware Download/Commit command
Enter the URL in the browser and display it on the page