Registry / http-networking / data-mocks-server

data-mocks-server

JSON →
library10.0.2jsnpmunverified

Data Mocks Server is a JavaScript/TypeScript library designed for creating a standalone mock HTTP server, contrasting with client-side libraries that intercept `fetch` or `XHR` requests. It's built on Express, allowing developers to define API responses and scenarios through configuration rather than network interception. The library is currently at version 10.0.2, with its latest update in July 2024, indicating active maintenance. Releases are not on a fixed schedule but appear when features or bug fixes are available, with significant gaps between major versions like v9 to v10. Key differentiators include its server-first approach, a built-in UI for dynamic scenario switching at runtime, and comprehensive TypeScript support. It's particularly useful for end-to-end testing, local development, and showcasing features without relying on a real backend.

npm install data-mocks-server
INSTALL
IMPORT
SIG · DATA-MOCKS-SERVER
D
data-mocks-server
http-networkingjavascriptv10.0.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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

run
import { run } from 'data-mocks-server';
const { run } = require('data-mocks-server'); // Valid CJS, but ESM is preferred in modern TS/JS projects.
The `run` function is the primary entry point for starting the mock server. While CommonJS `require` works, ESM `import` is recommended for TypeScript and modern JavaScript environments.
createExpressApp
import { createExpressApp } from 'data-mocks-server';
import createExpressApp from 'data-mocks-server'; // Not a default export.
Use `createExpressApp` if you need direct access to the underlying Express application instance for custom middleware or advanced configurations, rather than letting `run` manage the server lifecycle.
Mock
import type { Mock } from 'data-mocks-server';
When working with TypeScript, import types like `Mock`, `HttpMock`, or `Response` using `import type` for clarity and to avoid bundling type definitions.

This quickstart demonstrates how to set up a mock server with default HTTP GET endpoints for users and products, including dynamic responses and delays. It also defines two scenarios ('emptyUsers' and 'errorProduct') that can be activated via the built-in UI to override default behaviors, and customizes the port and UI path.

import { run } from 'data-mocks-server'; const server = run({ default: [ { url: '/api/users', method: 'GET', response: { data: [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }] }, delay: 500 // Simulate network latency }, { url: '/api/products/:id', method: 'GET', response: (request) => { const productId = request.params.id; return { status: 200, data: { id: productId, name: `Product ${productId}`, price: 19.99 } }; }, headers: { 'X-Custom-Header': 'Mocked-Response' } } ], scenarios: { emptyUsers: [ { url: '/api/users', method: 'GET', response: { data: [] } } ], errorProduct: [ { url: '/api/products/:id', method: 'GET', response: { status: 500, data: { message: 'Internal Server Error' } } } ] }, options: { port: 4000, uiPath: '/admin-mocks' } }); console.log('Mock server running on http://localhost:4000'); console.log('Admin UI available at http://localhost:4000/admin-mocks'); // To stop the server programmatically (e.g., in tests) // setTimeout(() => { // server.kill(() => console.log('Mock server stopped.')); // }, 10000);
Debug
Known issues
breakingThe API for defining mock responses underwent a significant change in version 9.3.2. Existing `response` configurations from prior versions may no longer be compatible.
fix
Review the new `Response` API documentation and update your mock definitions accordingly. The `response` property may now accept a function or a more structured object for status, headers, and data.
affects: >=9.3.2
breakingVersion 10.0.0 introduced a breaking change as indicated by 'Force package upgrade' and a reference to the v9.3.2 changes. While specific new API changes in 10.0.0 itself aren't detailed, upgrading from pre-9.3.2 versions directly to 10.0.0 will certainly encounter the response API changes.
fix
Always consult the changelog for major version bumps. If migrating from pre-9.3.2, ensure all response configurations are updated to the new format.
affects: >=10.0.0
gotchaThe `v9.3.2` release was initially marked as 'Deprecated' with a note 'Published to incorrect version'. While functionally fixed, users might have encountered temporary issues with that specific version number in their package managers or registries.
fix
Always use the latest stable patch release (e.g., `^10.0.0`) to ensure you're on the most recent, non-deprecated version. If you specifically installed `9.3.2`, consider upgrading.
affects: =9.3.2
gotchaBy default, the server runs on port 3000 and the UI is accessible at the root path '/'. If another service is already using port 3000, the server will fail to start. Also, if your application has a root path handler, it might conflict with the mock server UI.
fix
Configure a different `port` or `uiPath` in the `options` object when calling `run`. For example, `{ options: { port: 4000, uiPath: '/_mocks' } }`.
affects: >=1.0.0
Errors
Common errors & fixes
Error: listen EADDRINUSE: address already in use :::3000
Another process is already using the default port (3000) where data-mocks-server attempts to start.
fix
Specify a different port in the `options` object: `run({ ..., options: { port: 4000 } })`.
TypeError: Cannot read properties of undefined (reading 'data') OR response.status is not a function
You are using an old `response` object structure (pre-v9.3.2) after upgrading to a newer version that changed the response API.
fix
Update your mock `response` definitions. Instead of `{ data: {...} }`, use `{ status: 200, data: {...}, headers: {...} }` or return an object with these properties from a response function.
Cannot find name 'run'.
Incorrect import statement or missing TypeScript configuration to recognize the module as ESM.
fix
Ensure you are using `import { run } from 'data-mocks-server';` in ESM contexts. If using CommonJS, `const { run } = require('data-mocks-server');`. For TypeScript, verify your `tsconfig.json` includes `"moduleResolution": "NodeNext"` or `"Node"` and `"esModuleInterop": true`.
Upgrade
Version history
10.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources