Registry / web-framework / mock-json-server

mock-json-server

JSON →
library1.3.6jsnpmunverified

Mock JSON Server is a Node.js package designed to create simple, configurable mock REST APIs using a single JSON file. Released as version 1.3.6, with its last update approximately six years ago, it offers a straightforward solution for frontend developers needing temporary API backends during development or for prototyping. Unlike more feature-rich alternatives that dynamically generate endpoints from top-level JSON keys, `mock-json-server` requires explicit path and HTTP method definitions within its configuration JSON. It provides basic CRUD-like capabilities by returning predefined data for specific routes and methods, and supports programmatic control (start, reload, stop) in addition to a command-line interface. Its primary differentiators are its minimal setup and explicit configuration, making it suitable for simple mocking scenarios where fine-grained control over static responses per route and method is desired.

npm install mock-json-server
INSTALL
IMPORT
SIG · MOCK-JSON-SERVER
M
mock-json-server
web-frameworkjavascriptv1.3.6
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

mockServer
import mockServer from 'mock-json-server'; // For ESM environments
const { mockServer } = require('mock-json-server');
The package primarily exposes its API via a default export in CommonJS, typically accessed as a single object containing `start`, `reload`, and `stop` methods. For ESM, a default import is used, but internally it's still a CommonJS module. Named imports are incorrect for this package.
start
const mockServer = require('mock-json-server'); mockServer.start(data, port);
import { start } from 'mock-json-server';
The `start` function is a method of the default exported `mockServer` object, not a top-level named export. It is used to initialize and run the mock server.
CLI Usage
npx mock-json-server data.json --port=3000
require('mock-json-server').cli();
While the package has a programmatic API, its most common usage is via the command-line interface, either through `npx` or a global installation (`npm install -g mock-json-server`). There is no direct programmatic 'cli' function to import.

This quickstart demonstrates how to programmatically start a mock server using `mock-json-server` with a predefined JSON data structure, serving multiple routes and HTTP methods. It initializes the server on a specified port and logs available endpoints.

const mockServer = require('mock-json-server'); const mockData = { '/api/users': { 'get': { 'data': [ { 'id': 1, 'name': 'Alice', 'email': 'alice@example.com' }, { 'id': 2, 'name': 'Bob', 'email': 'bob@example.com' } ] }, 'post': { 'data': { 'message': 'User created successfully', 'status': 'success' } } }, '/api/products/1': { 'get': { 'data': { 'id': 1, 'name': 'Laptop', 'price': 1200 } } } }; const PORT = process.env.PORT ?? 8000; mockServer.start(mockData, parseInt(PORT, 10)); console.log(`Mock JSON Server running on http://localhost:${PORT}`); console.log('GET /api/users - returns a list of users'); console.log('POST /api/users - returns a success message'); console.log('GET /api/products/1 - returns a single product'); // To stop the server programmatically after some time (optional) // setTimeout(() => { // mockServer.stop(); // console.log('Mock JSON Server stopped.'); // }, 60000);
mock-json-server --version
Debug
Known issues
deprecatedThe `mock-json-server` package has not been updated in over six years (last published July 2020). While functional for its intended purpose, it lacks modern features, bug fixes, or security updates compared to actively maintained alternatives.
fix
For new projects or if advanced features like dynamic routing, database persistence, or middleware are required, consider actively maintained alternatives like `json-server` (typicode) or `Mock Service Worker`.
affects: >=1.0.0
gotchaThe data structure for defining endpoints is very specific. Routes must be explicitly defined as keys, with nested objects for HTTP methods (e.g., `'/path': { 'get': { 'data': [...] } }`). It does not automatically generate RESTful endpoints from top-level JSON keys like some other mock servers.
fix
Ensure your `data.json` or programmatic data object strictly follows the `{ '/path': { 'method': { 'data': ... } } }` format for each desired endpoint and method. Incorrect structures will result in 404s or unexpected responses.
affects: >=1.0.0
gotchaThis tool is intended for local development and testing only. It lacks built-in authentication, authorization, or other production-grade security features. Exposing it to the public internet is a significant security risk.
fix
Never deploy `mock-json-server` to a production environment. For development, ensure it's only accessible from trusted networks or behind appropriate firewall rules.
affects: >=1.0.0
gotchaThe `reload` function, while present in the programmatic API, does not offer hot-reloading for changes made directly to the `data.json` file when running via CLI. It's intended for programmatic data updates.
fix
When using the CLI (`npx mock-json-server data.json`), changes to `data.json` require a manual restart of the server to take effect. If programmatic reloading is needed, use the `mockServer.reload(newData)` function directly in your script.
affects: >=1.0.0
Errors
Common errors & fixes
Error: listen EADDRINUSE: address already in use :::8000
The specified port (default 8000) is already being used by another application on your system.
fix
Use the `--port` flag (e.g., `mock-json-server data.json --port=3001`) or set the `PORT` environment variable (`mockServer.start(data, parseInt(process.env.PORT || '3000', 10));`) to use a different, available port.
Cannot GET /some/undefined/route
The requested URL path or HTTP method is not defined in your `data.json` or programmatic mock data object.
fix
Verify that the requested path and HTTP method (e.g., GET, POST) are explicitly defined in your mock data, following the expected `{ '/path': { 'method': { 'data': ... } } }` structure.
SyntaxError: Unexpected token 'X' in JSON at position Y
The `data.json` file contains invalid JSON syntax (e.g., missing commas, unquoted keys, comments).
fix
Correct the JSON syntax in your `data.json` file. Use a JSON linter or validator to identify and fix errors. Ensure it's pure JSON, not JSON5 or JavaScript objects.
Upgrade
Version history
1.3.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Resources
mock-json-server — npm install mock-json-server · libregistry