{"id":22835,"library":"vite-plugin-simple-json-server","title":"vite-plugin-simple-json-server","description":"A Vite plugin that provides a file-based mock API using JSON files during development. Version 0.6.2 (Feb 2025) is the latest stable release, published under MIT license. It offers zero-config CRUD endpoints from JSON files placed in a 'mock' directory, with built-in pagination, sorting, filtering (including operators like ne, lt, gt), and OpenAPI/Swagger UI support. Unlike heavier mock servers, it integrates directly into Vite's dev server, requires no database, and ships TypeScript types. It also serves static files and allows custom route handlers. Node >=15.7.0 required; Vite ^4.0.0 peer dependency.","status":"active","version":"0.6.2","language":"javascript","source_language":"en","source_url":"https://github.com/alextim/vite-plugin-simple-json-server","tags":["javascript","vite","plugin","vite-plugin","json","api","server","typescript"],"install":[{"cmd":"npm install vite-plugin-simple-json-server","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-simple-json-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-simple-json-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: plugin injects middleware into Vite dev/preview server","package":"vite","optional":false}],"imports":[{"note":"Default export is the Vite plugin function. CommonJS require works but TypeScript may need esModuleInterop.","symbol":"default","correct":"import jsonServer from 'vite-plugin-simple-json-server'"},{"note":"TypeScript type for plugin options; can also be imported as a named type.","symbol":"SimpleJsonServerOptions","correct":"import type { SimpleJsonServerOptions } from 'vite-plugin-simple-json-server'"},{"note":"The plugin returns a Vite Plugin object; the default export is a function that returns the plugin.","wrong":"const jsonServer = require('vite-plugin-simple-json-server') as any","symbol":"Plugin","correct":"import { Plugin } from 'vite'; import jsonServer from 'vite-plugin-simple-json-server'"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite';\nimport jsonServer from 'vite-plugin-simple-json-server';\n\nexport default defineConfig({\n  plugins: [jsonServer()],\n});\n\n// mock/products.json\n// [\n//   { \"id\": 1, \"name\": \"Banana\", \"price\": 2, \"weight\": 1 },\n//   { \"id\": 2, \"name\": \"Apple\", \"price\": 1.5, \"weight\": 0.8 }\n// ]\n\n// After starting dev server:\n// curl http://localhost:5173/products -> returns the full array\n// curl \"http://localhost:5173/products?sort=-price\" -> sorted by price descending\n// curl \"http://localhost:5173/products?name=Apple\" -> filtered by name (exact match)\n// curl \"http://localhost:5173/products?offset=0&limit=1\" -> paginated, first item\n// curl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"Orange\",\"price\":3}' http://localhost:5173/products -> creates a new item\n// Swagger UI at http://localhost:5173/__swagger","lang":"typescript","description":"Minimal setup for file-based mock API with Vite, including CRUD, pagination, sorting, filtering."},"warnings":[{"fix":"Use HEAD /collection or /collection?filter=value to get count via X-Total-Count header instead of ?count=true.","message":"v0.6.0: 'count' query parameter replaced by HEAD method. Existing code using ?count= will break.","severity":"breaking","affected_versions":">=0.6.0"},{"fix":"Rename option from 'mockRootDir' to 'mockDir' in vite config.","message":"v0.5.0: mockRootDir option renamed to mockDir. Existing configs using mockRootDir will be ignored.","severity":"breaking","affected_versions":">=0.5.0"},{"fix":"Ensure non-array JSON files are not accidentally mutated via POST/PUT/PATCH requests if unintended; consider adding custom handlers or restricting methods.","message":"v0.5.0: POST, PUT, PATCH operations now also work for non-array JSON. Previously they only worked for arrays. This may affect custom behavior relying on read-only non-array endpoints.","severity":"breaking","affected_versions":">=0.5.0"},{"fix":"Double-check field names in query parameters; use exact field names from the JSON data.","message":"Query parameters for sorting/filtering that do not match any JSON field are silently ignored. Misspelled field names may go unnoticed.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure mock JSON files for collection endpoints are arrays of objects; for single resources, use object files without array wrapper.","message":"Pagination (offset/limit) and count only work for JSON files that contain arrays at the top level. Single objects or nested data will not paginate.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use Vite 4.x; check latest version for Vite 5 support.","message":"The plugin only works with Vite ^4.0.0. Incompatible with Vite 5+ until updated.","severity":"deprecated","affected_versions":">=0.0.0 <0.7.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Downgrade Vite to 4.x, or wait for plugin update that supports Vite 5.","cause":"Plugin peer dependency restricts to Vite 4.x.","error":"Error: The plugin 'vite-plugin-simple-json-server' requires Vite ^4.0.0 but current Vite version is 5.x"},{"fix":"Use import statement (native ESM) or enable experimental ESM support in Node.","cause":"Package is ESM-only but being used in a CJS context.","error":"Error: require() of ES Module vite-plugin-simple-json-server not supported. Instead change the require to a dynamic import."},{"fix":"Ensure all JSON files are valid and start with '[' or '{'. Remove BOM characters.","cause":"File in mockDir is not valid JSON or has BOM.","error":"JSON at position 0 unexpected token"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}