{"id":13058,"library":"data-mocks-server","title":"Data Mocks Server","description":"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.","status":"active","version":"10.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/kenneth-gray/data-mocks-server","tags":["javascript","mocks","server","typescript"],"install":[{"cmd":"npm install data-mocks-server","lang":"bash","label":"npm"},{"cmd":"yarn add data-mocks-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add data-mocks-server","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const { run } = require('data-mocks-server'); // Valid CJS, but ESM is preferred in modern TS/JS projects.","symbol":"run","correct":"import { run } from 'data-mocks-server';"},{"note":"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.","wrong":"import createExpressApp from 'data-mocks-server'; // Not a default export.","symbol":"createExpressApp","correct":"import { createExpressApp } from 'data-mocks-server';"},{"note":"When working with TypeScript, import types like `Mock`, `HttpMock`, or `Response` using `import type` for clarity and to avoid bundling type definitions.","symbol":"Mock","correct":"import type { Mock } from 'data-mocks-server';"}],"quickstart":{"code":"import { run } from 'data-mocks-server';\n\nconst server = run({\n  default: [\n    {\n      url: '/api/users',\n      method: 'GET',\n      response: { data: [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }] },\n      delay: 500 // Simulate network latency\n    },\n    {\n      url: '/api/products/:id',\n      method: 'GET',\n      response: (request) => {\n        const productId = request.params.id;\n        return { status: 200, data: { id: productId, name: `Product ${productId}`, price: 19.99 } };\n      },\n      headers: { 'X-Custom-Header': 'Mocked-Response' }\n    }\n  ],\n  scenarios: {\n    emptyUsers: [\n      { url: '/api/users', method: 'GET', response: { data: [] } }\n    ],\n    errorProduct: [\n      { url: '/api/products/:id', method: 'GET', response: { status: 500, data: { message: 'Internal Server Error' } } }\n    ]\n  },\n  options: {\n    port: 4000,\n    uiPath: '/admin-mocks'\n  }\n});\n\nconsole.log('Mock server running on http://localhost:4000');\nconsole.log('Admin UI available at http://localhost:4000/admin-mocks');\n\n// To stop the server programmatically (e.g., in tests)\n// setTimeout(() => {\n//   server.kill(() => console.log('Mock server stopped.'));\n// }, 10000);\n","lang":"typescript","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":">=9.3.2"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=10.0.0"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"=9.3.2"},{"fix":"Configure a different `port` or `uiPath` in the `options` object when calling `run`. For example, `{ options: { port: 4000, uiPath: '/_mocks' } }`.","message":"By 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Specify a different port in the `options` object: `run({ ..., options: { port: 4000 } })`.","cause":"Another process is already using the default port (3000) where data-mocks-server attempts to start.","error":"Error: listen EADDRINUSE: address already in use :::3000"},{"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.","cause":"You are using an old `response` object structure (pre-v9.3.2) after upgrading to a newer version that changed the response API.","error":"TypeError: Cannot read properties of undefined (reading 'data') OR response.status is not a function"},{"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`.","cause":"Incorrect import statement or missing TypeScript configuration to recognize the module as ESM.","error":"Cannot find name 'run'."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}