express-mock-middleware
raw JSON → 0.0.6 verified Sat Apr 25 auth: no javascript
A simple mock middleware for Express.js that allows you to define mock API responses in separate JavaScript files. Version 0.0.6 is the latest and stable release. It uses a glob pattern to load mock files (e.g., 'mock/**/*.js') where each file exports an object mapping HTTP methods and paths to handler functions. The middleware intercepts matching requests and returns mock data without requiring a real backend. It is a lightweight alternative to full mocking libraries, focusing on simplicity and ease of use. The package has minimal dependencies and is suitable for development and testing environments.
Common errors
error TypeError: mockMiddleware is not a function ↓
cause Destructuring the default export as a named import: const { mockMiddleware } = require('express-mock-middleware');
fix
Use: const mockMiddleware = require('express-mock-middleware'); or import mockMiddleware from 'express-mock-middleware';
error Cannot find module 'globby' ↓
cause Missing dependency globby is not installed automatically in some environments.
fix
Run: npm install globby --save-dev
error Mock file not loaded: no match for pattern 'mock/**/*.js' ↓
cause The glob pattern does not match any files because the cwd is incorrect or the mock directory doesn't exist.
fix
Pass cwd: __dirname in options and ensure the mock directory exists relative to that path.
error Route /api/info returned 404 ↓
cause Mock file key format is incorrect (e.g., 'GET/api/info' without space) or file not loaded.
fix
Ensure key is exactly 'GET /api/info' (space between method and path).
Warnings
gotcha Glob pattern is relative to process.cwd() by default; use cwd option to change base directory. ↓
fix Pass cwd: __dirname in options to base path on the current file.
gotcha Mock files must export an object with keys like 'METHOD /path'. Missing space causes routing failure. ↓
fix Use exact format: 'GET /api/info' or 'POST /api/submit'.
gotcha Only supports synchronous handlers; async/await or Promises not supported. ↓
fix Use callback-style middleware or provide a synchronous response.
deprecated Package has not been updated since 2015 and may contain security vulnerabilities in dependencies. ↓
fix Consider using a more actively maintained mocking library like 'express-mock-api' or 'msw'.
gotcha Middleware overwrites routes; define mock middleware before other route handlers. ↓
fix Ensure app.use(mockMiddleware(...)) is placed before app.get(...) or other routes.
Install
npm install express-mock-middleware yarn add express-mock-middleware pnpm add express-mock-middleware Imports
- default wrong
const mockMiddleware = require('express-mock-middleware')correctimport mockMiddleware from 'express-mock-middleware' - mockMiddleware wrong
const { mockMiddleware } = require('express-mock-middleware')correctconst mockMiddleware = require('express-mock-middleware') - type wrong
import { default as mockMiddleware } from 'express-mock-middleware'correctimport mockMiddleware from 'express-mock-middleware'
Quickstart
const express = require('express');
const mockMiddleware = require('express-mock-middleware');
const path = require('path');
const app = express();
// Define mock files in a directory, e.g., mock/
app.use(mockMiddleware({
glob: 'mock/**/*.js', // relative to process.cwd()
cwd: __dirname // optional, resolve from current directory
}));
app.listen(3000, () => {
console.log('Server running on port 3000');
});
// Example mock file: mock/info.js
// module.exports = {
// 'GET /api/info': (req, res) => {
// res.json({ success: true, data: { name: 'test' } });
// }
// };