Registry / testing / express-mock-server

express-mock-server

JSON →
library3.4.3jsnpmunverified

A lightweight mock server built on top of Express.js for rapidly creating fake API endpoints during development and testing. Current stable version is 3.4.3 with an active release cadence. Key differentiators include a declarative JSON-based routing configuration, support for dynamic responses via functions, and an optional control API for runtime manipulation.

npm install express-mock-server
INSTALL
IMPORT
SIG · EXPRESS-MOCK-SERVE
E
express-mock-server
testingjavascriptv3.4.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

serverStart
import { serverStart } from 'express-mock-server'
const serverStart = require('express-mock-server')
ESM-only since v3. CommonJS require will not work.
Server
import type { Server } from 'express-mock-server' (TypeScript)
import { Server } from 'express-mock-server' (JavaScript runtime error)
Server is a TypeScript type, not a value. Only import it with 'type' keyword in TS.
serverStart
const { serverStart } = await import('express-mock-server')
const { serverStart } = require('express-mock-server')
Dynamic import works in CJS environments, but static require is not supported.
default
import mockServer from 'express-mock-server'
const mockServer = require('express-mock-server')
Default export is the same as named 'serverStart'.

Start a mock server with a single route returning a JSON array. Uses ESM import and demonstrates basic source file format.

// mock-source.js module.exports = [ { request: { method: 'GET', path: '/api/users' }, response: { statusCode: 200, body: JSON.stringify([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]) } } ]; // app.js import { serverStart } from 'express-mock-server'; const sources = [require('./mock-source.js')]; const server = serverStart(sources, { port: 3000, controlApiUrl: '/_api' }); console.log('Mock server running on port 3000'); // To stop: server.close();
Debug
Known issues
breakingVersion 3.x drops CommonJS support; only ESM imports are allowed.
fix
Use import syntax or dynamic import(). If you must use require, stick to v2.x.
affects: >=3.0.0
gotchaThe 'body' field in response must be a string; JSON.stringify() is required.
fix
Ensure response.body is a string, e.g., JSON.stringify({ key: 'value' }).
affects: >=0.0.0
gotchaIf you omit the request.method, the route may match any HTTP method.
fix
Always specify method: 'GET', 'POST', etc. to avoid unintended matches.
affects: >=0.0.0
deprecatedPassing an array of arrays (nested sources) is deprecated and will be removed in v4.
fix
Flatten your sources to a single array of route objects.
affects: >=3.0.0
Errors
Common errors & fixes
Cannot find module 'express-mock-server'
Missing installation or using wrong import style for v3.
fix
Run 'npm install express-mock-server' and use 'import { serverStart } from 'express-mock-server''.
TypeError: mockServer is not a function
Default import used with CommonJS require.
fix
Use 'import { serverStart } from 'express-mock-server'' or 'const { serverStart } = await import('express-mock-server')'.
Uncaught SyntaxError: The requested module 'express-mock-server' does not provide an export named 'serverStart'
Using named import with CommonJS require in a non-ESM context.
fix
Switch to ESM (e.g., type: 'module' in package.json) or use dynamic import.
Upgrade
Version history
3.4.3latest on npm
Audit
Dependencies
expressrequiredCore HTTP server framework
Agent activity
2 hits · last 30 days
node
2
Resources
express-mock-server — npm install express-mock-server · libregistry