Registry / devops / express-ws

express-ws

JSON →
library5.0.2jsnpmunverified

Adds WebSocket endpoints to Express applications using the ws library. Version 5.0.2 (latest stable) is maintained with updates as needed. Express-ws enables defining WebSocket routes with standard Express middleware support, working seamlessly with Express routers. It modifies the global Router prototype by default and supports custom server instances and WebSocket options. The package requires Express peer dependency (express ^4.0.0 || ^5.0.0-alpha.1) and Node >=4.5.0. Key differentiator: minimal syntax compared to raw ws usage, but suffers from prototype monkey-patching and thread-unsafe design.

npm install express-ws
INSTALL
IMPORT
SIG · EXPRESS-WS
E
express-ws
devopsjavascriptv5.0.2
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.

default
const expressWs = require('express-ws')(app);
import expressWs from 'express-ws'; expressWs(app); // ESM import not supported; must use require or dynamic import
express-ws is CJS-only. Use require or await import('express-ws') then call the default export.
expressWs.applyTo
const expressWs = require('express-ws')(app); expressWs.applyTo(router);
const { applyTo } = require('express-ws'); applyTo(router); // applyTo is not a named export; it's a method on the returned instance
You must first initialize with the app, then call applyTo on the returned object.
wsInstance.getWss
const expressWs = require('express-ws')(app); const wss = expressWs.getWss();
const { getWss } = require('express-ws'); getWss(); // same issue: not a named export
getWss returns the underlying WebSocketServer instance. Access clients via wss.clients.

Creates an Express app, adds WebSocket support, and defines an echo WebSocket endpoint at /echo.

const express = require('express'); const app = express(); const expressWs = require('express-ws')(app); app.ws('/echo', (ws, req) => { ws.on('message', msg => { ws.send(`echo: ${msg}`); }); }); app.listen(3000, () => console.log('Server running on port 3000'));
Debug
Known issues
breakingexpress-ws modifies the global Router prototype. This affects all subsequent Router instances created with express.Router(), including those from other modules.
fix
Use option `leaveRouterUntouched: true` and manually call `expressWs.applyTo(router)` on each router you want WebSocket support. Or consider using ws directly.
affects: >=3.0.0
gotchaCalling `require('express-ws')(app)` after defining routes/routers will break. express-ws must be initialized before any routers that use .ws.
fix
Initialize express-ws immediately after creating the app, before any router definitions or app.use calls.
affects: all
deprecatedThe ws library's `clients` property is not safe for iteration in production; it is a Set but can change size during iteration. express-ws does not protect against this.
fix
Clone the set before iterating: const clients = [...wss.clients]; clients.forEach(...).
affects: >=4.0.0
gotchaexpress-ws does not support passing options to the WebSocket server per route; the wsOptions in constructor apply globally. Cannot set maxPayload etc per endpoint.
fix
Initialize with wsOptions: { maxPayload: 100 * 1024 } for global settings, or handle per-connection in the callback.
affects: all
deprecatedThe default export pattern (require('express-ws')(app)) is common but not tree-shakable and prevents static analysis.
fix
Use const expressWs = await import('express-ws'); expressWs.default(app); but note it's still CJS under the hood.
affects: >=5.0.0
Errors
Common errors & fixes
TypeError: app.ws is not a function
express-ws not initialized before calling app.ws.
fix
Ensure require('express-ws')(app) is called before any route definitions.
TypeError: router.ws is not a function
express-ws initialized after creating the router, or `leaveRouterUntouched: true` without calling applyTo.
fix
Initialize express-ws first, or call expressWs.applyTo(router).
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
Trying to use ESM import syntax with express-ws (CJS only).
fix
Use require('express-ws') or dynamic import with await import('express-ws').
Upgrade required for websocket connection - handshake failed
Server is behind a proxy or load balancer that doesn't handle WebSocket upgrade.
fix
Configure the proxy to forward Upgrade headers, or use the 'server' option in expressWs to pass the raw http.Server.
Upgrade
Version history
5.0.2latest on npm
Audit
Dependencies
expressrequiredExpress is a peer dependency; the module adds .ws method to Express app and Router prototypes.
wsrequiredWebSocket library used under the hood for actual WebSocket server implementation.
Agent activity
2 hits · last 30 days
node
2
Resources
express-ws — npm install express-ws · libregistry