当前位置:网站首页>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 预览无异常。
边栏推荐
- Dart tips
- Redis设计与实现(五)| Sentinel哨兵
- Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
- Graffiti Wi Fi & ble SoC development slide strip
- Cesium learning notes (IV) visual image & Terrain
- Introduction to opencv (I): image reading and display
- Pycharm Dlib library installation
- 电流探头电路分析
- Miracle Mu server rental selection is real and easy to use, stable and intrusion proof
- layer.open 当传值为数组或值太长时处理方法
猜你喜欢
Sword finger offer II 074 Merge interval (sort, array)
云服务器上部署仿牛客网项目
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
Interference source current spectrum test of current probe
Full stack performance testing theory - Summary
增强for循环的增删操作 & 迭代器删除集合元素
Circuit analysis of current probe
【NVMe2.0b 14-5】Firmware Download/Commit command
示波器探头对测量电容负荷有影响吗?
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
随机推荐
小心transmittable-thread-local的这个坑
Redis设计与实现(五)| Sentinel哨兵
Does the oscilloscope probe affect the measurement of capacitive load?
C # listbox how to get the selected content (search many invalid articles)
php api获取二维码、组合生成图片
Build a docker image of Henkel database from 0
Sword finger offer II 075 Array relative sort (custom sort, count sort)
Opencv video
Conversion between map, string and JSON
[flower carving experience] 14 line blank board pingpong library test external sensor module (one)
Transformer architecture understanding
Acreems energy efficiency management platform escorts the power safety of high-rise residential areas
Qqquickpainteditem implements graffiti program drawing board
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
Interference source current spectrum test of current probe
What are the Amazon evaluation terms?
Miracle Mu server rental selection is real and easy to use, stable and intrusion proof
Development technology sharing of Jingtan NFT digital collection system
Introduction to opencv (II): image color space conversion and image saving
Construction of energy conservation supervision system for campus buildings of ankery University