vite-plugin-mock-dev-server is a Vite plugin designed to provide a lightweight, flexible, and fast API mock development server. It is currently stable at version 2.1.1 and receives frequent updates, including new features and bug fixes. Key differentiators include its non-intrusive, non-injection-based approach to mocking, full TypeScript support, Hot Module Replacement (HMR) for mock files, and pure ES Module architecture since version 2.0.0. The plugin automatically imports mock files from designated directories (default `mock` folder) and supports various content types for responses (text, JSON, buffer, stream). It integrates seamlessly with Vite's `server.proxy` configuration and allows the use of `viteConfig.define` and environment variables within mock definitions. Advanced features like WebSocket mocking, request recording/replay, error simulation, and the ability to build small, independent deployable mock services further enhance its utility for front-end development workflows.
npm install vite-plugin-mock-dev-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `vite-plugin-mock-dev-server` in `vite.config.ts` and define mock API endpoints using `defineMock` within a mock file. It shows basic GET requests, path parameter handling, response delays, error simulation, and HMR persistence.
Migrate all imports from `require('vite-plugin-mock-dev-server')` to `import mockDevServerPlugin from 'vite-plugin-mock-dev-server'`. Ensure your Vite configuration file (`vite.config.ts` or `.js`) is configured for ESM.Review existing mock `url` patterns, especially those with advanced regex or segment definitions, and update them according to `path-to-regexp` v8 specifications. Test all mock routes thoroughly.
Explicitly set the `dir` option in the plugin configuration if your mock files are not in the default `mock` directory (e.g., `mockDevServerPlugin({ dir: 'src/mocks' })`). Verify that your `include` and `exclude` patterns, if used, correctly resolve against the new base directory.Ensure your development environment uses Node.js version 20 or greater, or 22 or greater. Update Node.js if necessary (e.g., using `nvm install 20 && nvm use 20`).
Add or verify `server.proxy` in your `vite.config.ts` (e.g., `server: { proxy: { '^/api': { target: 'http://localhost:3000' } } }`). Ensure the proxy prefix matches the `url` patterns defined in your mock files.Ensure your `vite.config.js` uses ES Module syntax (e.g., `import mockDevServerPlugin from 'vite-plugin-mock-dev-server'`) and potentially change the file extension to `.mjs` or ensure your Node.js environment correctly handles ESM.
Ensure every mock file exports its definitions using `export default defineMock({...})` or `export default defineMock([...])`.Verify that `server.proxy` in `vite.config.ts` includes a rule that matches `/api/my-endpoint` (e.g., `'^/api': { target: '...' }`). Also, check the `url` property in your `defineMock` call to ensure it precisely matches the requested path, including any path parameters.