Registry / web-framework / vite-plugin-simple-json-server

vite-plugin-simple-json-server

JSON →
library0.6.2jsnpmunverified

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.

npm install vite-plugin-simple-json-server
INSTALL
IMPORT
SIG · VITE-PLUGIN-SIMPLE
V
vite-plugin-simple-json-server
web-frameworkjavascriptv0.6.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import jsonServer from 'vite-plugin-simple-json-server'
Default export is the Vite plugin function. CommonJS require works but TypeScript may need esModuleInterop.
SimpleJsonServerOptions
import type { SimpleJsonServerOptions } from 'vite-plugin-simple-json-server'
TypeScript type for plugin options; can also be imported as a named type.
Plugin
import { Plugin } from 'vite'; import jsonServer from 'vite-plugin-simple-json-server'
const jsonServer = require('vite-plugin-simple-json-server') as any
The plugin returns a Vite Plugin object; the default export is a function that returns the plugin.

Minimal setup for file-based mock API with Vite, including CRUD, pagination, sorting, filtering.

// vite.config.ts import { defineConfig } from 'vite'; import jsonServer from 'vite-plugin-simple-json-server'; export default defineConfig({ plugins: [jsonServer()], }); // mock/products.json // [ // { "id": 1, "name": "Banana", "price": 2, "weight": 1 }, // { "id": 2, "name": "Apple", "price": 1.5, "weight": 0.8 } // ] // After starting dev server: // curl http://localhost:5173/products -> returns the full array // curl "http://localhost:5173/products?sort=-price" -> sorted by price descending // curl "http://localhost:5173/products?name=Apple" -> filtered by name (exact match) // curl "http://localhost:5173/products?offset=0&limit=1" -> paginated, first item // curl -X POST -H "Content-Type: application/json" -d '{"name":"Orange","price":3}' http://localhost:5173/products -> creates a new item // Swagger UI at http://localhost:5173/__swagger
Debug
Known issues
breakingv0.6.0: 'count' query parameter replaced by HEAD method. Existing code using ?count= will break.
fix
Use HEAD /collection or /collection?filter=value to get count via X-Total-Count header instead of ?count=true.
affects: >=0.6.0
breakingv0.5.0: mockRootDir option renamed to mockDir. Existing configs using mockRootDir will be ignored.
fix
Rename option from 'mockRootDir' to 'mockDir' in vite config.
affects: >=0.5.0
breakingv0.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.
fix
Ensure non-array JSON files are not accidentally mutated via POST/PUT/PATCH requests if unintended; consider adding custom handlers or restricting methods.
affects: >=0.5.0
gotchaQuery parameters for sorting/filtering that do not match any JSON field are silently ignored. Misspelled field names may go unnoticed.
fix
Double-check field names in query parameters; use exact field names from the JSON data.
affects: >=0.0.0
gotchaPagination (offset/limit) and count only work for JSON files that contain arrays at the top level. Single objects or nested data will not paginate.
fix
Ensure mock JSON files for collection endpoints are arrays of objects; for single resources, use object files without array wrapper.
affects: >=0.0.0
deprecatedThe plugin only works with Vite ^4.0.0. Incompatible with Vite 5+ until updated.
fix
Use Vite 4.x; check latest version for Vite 5 support.
affects: >=0.0.0 <0.7.0
Errors
Common errors & fixes
Error: The plugin 'vite-plugin-simple-json-server' requires Vite ^4.0.0 but current Vite version is 5.x
Plugin peer dependency restricts to Vite 4.x.
fix
Downgrade Vite to 4.x, or wait for plugin update that supports Vite 5.
Error: require() of ES Module vite-plugin-simple-json-server not supported. Instead change the require to a dynamic import.
Package is ESM-only but being used in a CJS context.
fix
Use import statement (native ESM) or enable experimental ESM support in Node.
JSON at position 0 unexpected token
File in mockDir is not valid JSON or has BOM.
fix
Ensure all JSON files are valid and start with '[' or '{'. Remove BOM characters.
Upgrade
Version history
0.6.2latest on npm
Audit
Dependencies
viterequiredPeer dependency: plugin injects middleware into Vite dev/preview server
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
1
Resources