{"id":15348,"library":"json-server-reset","title":"json-server-reset: Middleware for Data Reset and Merge","description":"json-server-reset is a middleware for the popular json-server library, enabling developers to programmatically reset or merge the in-memory database of a running mock API. The current stable version is 1.6.4. Releases are infrequent but consistent, primarily driven by dependency updates and occasional feature additions, as seen with features like 'merge mode' and 'reset module' in v1.5.0 and v1.6.0. Its key differentiator is providing a simple HTTP endpoint (`/reset` or `/merge`) to modify the mock database state during testing or development, which is crucial for maintaining consistent test environments or quickly iterating on frontend changes without restarting the mock server. It integrates directly into the json-server middleware chain, requiring no complex setup beyond standard Express middleware practices. This package is vital for scenarios where a dynamic, resettable mock backend is needed, especially in automated testing suites or rapid prototyping.","status":"active","version":"1.6.4","language":"javascript","source_language":"en","source_url":"https://github.com/bahmutov/json-server-reset","tags":["javascript","json-server","middleware","reset"],"install":[{"cmd":"npm install json-server-reset","lang":"bash","label":"npm"},{"cmd":"yarn add json-server-reset","lang":"bash","label":"yarn"},{"cmd":"pnpm add json-server-reset","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency for providing the mock API server functionality that this middleware extends.","package":"json-server","optional":false}],"imports":[{"note":"Primarily used in CommonJS style within Node.js servers. The package does not explicitly provide an ESM export for the main middleware, so `require` is the safest option.","wrong":"import reset from 'json-server-reset';","symbol":"reset","correct":"const reset = require('json-server-reset');"},{"note":"The 'merge' functionality is exposed as a separate middleware file within the `src` directory, requiring a direct path. It is not a named export from the main package entry point.","wrong":"import { merge } from 'json-server-reset';","symbol":"mergeMiddleware","correct":"const mergeMiddleware = require('json-server-reset/src/merge');"},{"note":"For initializing resource clearing on server startup, this helper is exposed from a specific file path. Like `merge`, it's not a direct named export.","wrong":"import { initReset } from 'json-server-reset';","symbol":"initJsonServerReset","correct":"const initJsonServerReset = require('json-server-reset/src/init-reset');"}],"quickstart":{"code":"const jsonServer = require('json-server');\nconst reset = require('json-server-reset');\nconst path = require('path');\nconst fs = require('fs');\n\nconst dataFilename = path.join(__dirname, 'data.json');\n// Create a simple initial data file if it doesn't exist\nif (!fs.existsSync(dataFilename)) {\n  fs.writeFileSync(dataFilename, JSON.stringify({ todos: [], users: [{ id: 1, name: 'Alice' }] }, null, 2));\n}\n\nconst server = jsonServer.create();\nconst router = jsonServer.router(dataFilename);\n\n// json-server-reset requires body-parser, which is included in jsonServer.defaults\nserver.use(\n  jsonServer.defaults({\n    static: '.', // optional static server folder\n    bodyParser: true,\n    readOnly: false,\n  })\n);\n\n// Attach the reset middleware before the json-server router\n// The middleware expects the router's db to be accessible via server.db\nserver.use(reset);\nserver.db = router.db;\nserver.use(router);\n\nconst PORT = 3000;\nserver.listen(PORT, () => {\n  console.log(`JSON Server with reset middleware is running on port ${PORT}`);\n  console.log(`Try:`);\n  console.log(`- GET http://localhost:${PORT}/todos`);\n  console.log(`- POST http://localhost:${PORT}/todos text='New Todo'`);\n  console.log(`- POST http://localhost:${PORT}/reset todos:=[] (using httpie)`);\n  console.log(`- POST http://localhost:${PORT}/reset users:='[{ \"id\": 2, \"name\": \"Bob\" }]' (using httpie)`);\n});","lang":"javascript","description":"Demonstrates setting up a `json-server` instance with `json-server-reset` middleware, allowing API data to be reset via a POST request to `/reset`."},"warnings":[{"fix":"Ensure `server.use(reset)` comes before `server.use(router)`. If not using `jsonServer.defaults({ bodyParser: true })`, manually add `server.use(express.json())` and `server.use(express.urlencoded({ extended: true }))` before `server.use(reset)`.","message":"The `json-server-reset` middleware must be placed *before* the `jsonServer.router` middleware in your Express application chain. It also requires the `body-parser` middleware to be active to parse incoming JSON payloads for the reset operation.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"After initializing `const router = jsonServer.router(dataFilename);`, add `server.db = router.db;` before `server.use(router);`.","message":"When integrating `json-server-reset` into a custom server setup, you must explicitly assign the `json-server` router's database instance to `server.db` for the reset middleware to function correctly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `const reset = require('json-server-reset');` for the main middleware. For `merge` and `init-reset`, use `require('json-server-reset/src/merge')` and `require('json-server-reset/src/init-reset')` respectively.","message":"The package currently uses CommonJS `require` for its main entry points and explicit paths for sub-modules like `merge` and `init-reset`. Attempting to use ESM `import` statements directly might fail depending on your build configuration or Node.js module resolution setup.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"After creating your `jsonServer.router`, add `server.db = router.db;` before applying the router middleware.","cause":"The `server.db` property, which the `json-server-reset` middleware expects to find, has not been assigned the `json-server` router's database instance.","error":"TypeError: Cannot read properties of undefined (reading 'db')"},{"fix":"Ensure you are sending a `POST` request to `/reset` or `/merge` and that the request body is valid JSON. Use tools like `httpie` with `key:=value` syntax or `curl -X POST -H \"Content-Type: application/json\" -d '{\"todos\":[]}'`.","cause":"The `json-server-reset` middleware received a request to the `/reset` or `/merge` endpoint without a valid JSON body, or with an incorrect HTTP method (e.g., GET).","error":"Error: reset requires a POST request with JSON body"}],"ecosystem":"npm"}