vue3使用mock插件vite-plugin-mock报错require is not defined,有点懵!!!

先看看错误信息吧。

yarn run v1.22.19
$ vite
  if (!require.cache) {
  ^

ReferenceError: require is not defined
    at cleanRequireCache (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite-plugin-mock/dist/index.mjs:128:3)
    at getMockConfig (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite-plugin-mock/dist/index.mjs:157:3)
    at createMockServer (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite-plugin-mock/dist/index.mjs:52:20)
    at configResolved (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite-plugin-mock/dist/index.mjs:246:16)
    at file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite/dist/node/chunks/dep-df561101.js:65946:28
    at Array.map (<anonymous>)
    at resolveConfig (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite/dist/node/chunks/dep-df561101.js:65946:14)
    at async _createServer (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite/dist/node/chunks/dep-df561101.js:64959:20)
    at async CAC.<anonymous> (file:///D:/%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85/honey_admin_ts/node_modules/vite/dist/node/cli.js:743:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

然后找到报错文件

在这个文件顶部加入

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

小兵仍在前行,英雄怎能放弃!