enjoycoding / vite-plugin-mock-server

A mock server plugin for Vite.
MIT License
58 stars 13 forks source link

"require is not defined" error when using plugin in ESM project #30

Closed johnhunter closed 8 months ago

johnhunter commented 9 months ago

Node: v20.11.0 Vite: 5.1.1 vite-plugin-mock-server: 1.1.2 Package:

{
  "type": "module",
}

Added the plugin to a vite project with the default mock module location. Starting the dev server resulted in a require is not defined error:

[vite-plugin-mock-server] [
  'watched root dir is',
  '[...]/mock'
]
[vite-plugin-mock-server] [
  'recursive loading mock modules',
  '[...]/mock'
]
[vite-plugin-mock-server] [
  'loading mock module',
  '[...]/mock/endpoints.mock.js'
]
[vite-plugin-mock-server] [
  'delete module cache',
  '[...]/mock/endpoints.mock.js'
]
file:///[...]/node_modules/vite-plugin-mock-server/dist/index.js:193
    delete require.cache[moduleName];
    ^

ReferenceError: require is not defined

It might be related to, or resolved by https://github.com/enjoycoding/vite-plugin-mock-server/pull/29 ?

Am unable to verify the example from the plugin repo as npm install gives the following error:

npm ERR! code CERT_HAS_EXPIRED
npm ERR! errno CERT_HAS_EXPIRED
npm ERR! request to https://registry.npm.taobao.org/to-regex-range/download/to-regex-range-5.0.1.tgz failed, reason: certificate has expired
tGrefsrud commented 9 months ago

29 resolves it, hopefully it will be merged soon. I tried just making the changes from the PR locally and it fixed the issue for me, but that is just a temp fix and not ideal in the long run.

johnhunter commented 9 months ago

Thanks @tGrefsrud Hopefully it will be merged and released soon! 🤞

octoape commented 8 months ago

PR https://github.com/enjoycoding/vite-plugin-mock-server/pull/29 has been merged, thank you for @reinerBa contribution.