当前位置:网站首页>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 預覽無异常。
边栏推荐
- Conversion between map, string and JSON
- Cesium learning notes (V) custom geometry and appearance
- Conversion between basic data types in go data types
- Markdown支持的emoji图标
- Redis 的过期数据如何处理,淘汰机制都有那些?
- Redis design and Implementation (IV) | master-slave replication
- 2021-02-19
- Redis设计与实现(二)| 数据库(删除策略&过期淘汰策略)
- 跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过
- C preliminary chapter learning route
猜你喜欢

Redis design and Implementation (II) | database (deletion strategy & expiration elimination strategy)

Wechat official account third-party platform development, zero foundation entry. I want to teach you
![[untitled]](/img/b8/e3f54fe5d1079663799887e62cb07c.jpg)
[untitled]

【NVMe2.0b 14-8】Set Features(下篇)

领域驱动下cloud项目中单个服务的示例

微信小程序使用vant weapp报错

Redis设计与实现(八)| 事务

Gilbert Strang's course notes on linear algebra - Lesson 1

跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过

Cesium learning notes (V) custom geometry and appearance
随机推荐
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
2021-02-18
Map,String,Json之間轉換
layer. Open processing method when the passed value is an array or the value is too long
2021-02-27
Circuit analysis of current probe
【NVMe2.0b 14-5】Firmware Download/Commit command
Enhance the add / delete operation of for loop & iterator delete collection elements
[JUC series] overview of fork/join framework
2021-02-22
【NVMe2.0b 14-4】Directive Send/Receive command
【NVMe2.0b 14】NVMe Admin Command Set
Want to ask, how to choose securities companies for stock speculation? Is it safe to open an account online?
Redis design and Implementation (IV) | master-slave replication
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
codeforces每日5题(均1700)-第三天
Development technology sharing of Jingtan NFT digital collection system
Miracle Mu server rental selection is real and easy to use, stable and intrusion proof
Gilbert Strang's course notes on linear algebra - Lesson 1