lws-mock-response
raw JSON → 2.0.0 verified Sat Apr 25 auth: no javascript
Middleware for adding mock responses to lws (local-web-server). Current stable version is 2.0.0, released under an active maintenance cycle. It enables offline development by simulating API responses when network services are unavailable. Mocks are defined in reusable modules. Key differentiators: tight integration with lws ecosystem, simple class-based mock definitions, and support for various response types (JSON, etc.). Ideal for frontend developers working offline or testing scenarios.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module not supported ↓
cause Using require() to load the package which is ESM-only in v2.
fix
Use import instead of require.
Warnings
breaking v2.0.0 dropped CommonJS support; all imports must use ESM. ↓
fix Update imports to use ESM syntax (import lwsMockResponse from 'lws-mock-response').
Install
npm install lws-mock-response yarn add lws-mock-response pnpm add lws-mock-response Imports
- default wrong
const lwsMockResponse = require('lws-mock-response')correctimport lwsMockResponse from 'lws-mock-response'
Quickstart
// Save as mocks.js
export default class MockRivers {
mocks () {
return {
route: '/rivers',
responses: [
{
response: {
type: 'json',
body: [
{ name: 'Volga', drainsInto: 'Caspian Sea' },
{ name: 'Danube', drainsInto: 'Black Sea' },
{ name: 'Ural', drainsInto: 'Caspian Sea' },
{ name: 'Dnieper', drainsInto: 'Black Sea' }
]
}
}
]
}
}
}
// Then run: ws --mocks mocks.js
// curl http://127.0.0.1:8000/rivers