vbenjs / vite-plugin-mock

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

NodeJS18+严重错误:if (!require.cache) { #120

Closed hc-advokate closed 9 months ago

hc-advokate commented 11 months ago

vite v5.x+ nodeJS v18.x+ package.json中"type":"module"

错误信息定位:index.mjs if (!require.cache) { ^

暂时解决方案如下: 第一种:当package.json中"type":"module"时:为修改此处代码,但这是一种破坏性行为,而且并非永久修改。

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

第二种:去掉package.json中"type":"module"。

未来vite6即将废弃cjs,所以在packages/vite-plugin-mock/src/createMockServer.ts中此处源码是否可以修改?

saonian commented 10 months ago

+1

jackzer666 commented 10 months ago

+1

FEADMilk commented 10 months ago

+1遇到了相同的问题,使用楼主第二种方法可以运行服务

Licheung228 commented 10 months ago

+1

chengyanqi123 commented 10 months ago

require不存在时,cleanRequireCache将失去本生作用(清除require缓存的对象),使cleanRequireCache不执行任何业务逻辑可以解暴力解决。

function cleanRequireCache(opt) {
  return void 0;
  // if (!require.cache) {
  //   return;
  // }
  // const { absConfigPath, absMockPath } = getPath(opt);
  // Object.keys(require.cache).forEach((file) => {
  //   if (file === absConfigPath || file.indexOf(absMockPath) > -1) {
  //     delete require.cache[file];
  //   }
  // });
}