vbenjs / vite-plugin-mock

A mock plugin for vite.use mockjs.
MIT License
610 stars 96 forks source link

运行yarn dev报错 vite版本4.2.1 ReferenceError: require is not defined #97

Closed L-T-G closed 8 months ago

L-T-G commented 1 year ago

image

badlym commented 1 year ago

我的也是,同问

wjw020206 commented 1 year ago

巧了,我这边vite版本4.2.2运行也遇到这个问题 image

wjw020206 commented 1 year ago

似乎和 vite 的版本没有关系,把 vite-plugin-mock 降到 2.9.8 就可以正常运行了

lincenying commented 1 year ago

"type": "module" 下, 不存在require, 导致报错误

jinzhirong commented 1 year ago

我今天也是这个问题,我是3.0.0,明天试试把type去掉看行不行

lincenying commented 1 year ago

我今天也是这个问题,我是3.0.0,明天试试把type去掉看行不行

想用3.0 可以先用 @lincy/vite-plugin-mock, 然后等作者更新吧

npm install @lincy/vite-plugin-mock --dev

然后将vite-plugin-mock替换成@lincy/vite-plugin-mock

mazhenxiao commented 1 year ago

image 相同的问题,因为采用的是commandjs语法,在vite的ems语法中不支持,尝试解决采用@rollup/plugin-commonjs组件但是依然无效,应该只是针对开发中混合方式引入的cmd语法模块才有用,在当前编译环境下并不支持。

vimianma commented 1 year ago

我也遇到这个问题,首先降了版本发现还是存在require的问题,后来把node_module中vite-plugin-mock的index.mjs删掉后就正常了,mock功能也能正常使用! 删掉index.mjs后

CodingAndSleeping commented 1 year ago

我也遇到了这个错误,我把package.json里的 type:module 删掉了就好了

lbw611 commented 1 year ago

3.0.0的版本,vite4.2.3我也遇到了这个错误。降了版本到2.9.8可以运行了

abc-0886kAX-code commented 1 year ago

vite-plugin-mock@2.9.8解决了 谢谢鸭 Snipaste_2023-05-26_15-01-26

cnbase commented 1 year ago

找到 /dist/index.mjs ,注释掉 cleanRequireCache(opt);

776488326 commented 1 year ago

点击提示的位置在if条件内容改为 if (typeof require === "undefined" || !require.cache)

image
catyumouse commented 1 year ago

@lincenying package.json 去掉了type:module可以运行了

songyipan commented 1 year ago

点击提示的位置在if条件内容改为 if (typeof require === "undefined" || !require.cache) image

牛逼

aitrx commented 12 months ago

109

package.json 去掉"type": "module",

liaohaoxiang commented 11 months ago

点击提示的位置在if条件内容改为 if (typeof require === "undefined" || !require.cache) image

去掉"type": "module"会损失esm, 用这个方法可以使用

ryuushihou commented 11 months ago

109 package.json 去掉"type": "module",

这个办法解决了 原因是什么呀