{"record":{"id":"4fa8987cd44f213c","repo":"moeru-ai/airi","slug":"no-websocket-constructor-is-available-pass-wscon","errorCode":null,"errorMessage":"No WebSocket constructor is available. Pass `wsConstructor` or use a connector.","messagePattern":"No WebSocket constructor is available\\. Pass `wsConstructor` or use a connector\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/better-ws/src/client/index.ts","lineNumber":872,"sourceCode":"      waiters.add(wait.emit)\n\n      void wait.promise.finally(() => {\n        waiters.delete(wait.emit)\n      }).catch(() => {})\n\n      return wait.promise\n    }\n  }\n\n  return client\n}\n\nfunction createSocketConnector(options: ClientUrlOptions): ClientConnector<string> {\n  return {\n    connect(events) {\n      const WsConstructor = options.wsConstructor ?? globalThis.WebSocket\n      if (!WsConstructor) {\n        throw new Error('No WebSocket constructor is available. Pass `wsConstructor` or use a connector.')\n      }\n\n      const ws = new WsConstructor(options.url, options.protocols)\n      return new Promise<ClientConnection<string>>((resolve, reject) => {\n        let opened = false\n        let failedBeforeOpen = false\n        ws.onopen = () => {\n          opened = true\n          resolve({\n            send: message => ws.send(message),\n            close: (code, reason) => ws.close(code, reason),\n          })\n        }\n        ws.onmessage = (event) => {\n          if (typeof event.data === 'string') {\n            events.message(event.data)\n            return\n          }","sourceCodeStart":854,"sourceCodeEnd":890,"githubUrl":"https://github.com/moeru-ai/airi/blob/677329427f32468c74b17f3ec47eeca4e05bec65/packages/better-ws/src/client/index.ts#L854-L890","documentation":"better-ws's built-in socket connector needs a WebSocket constructor: it uses options.wsConstructor when provided, otherwise globalThis.WebSocket. When neither exists — typical for Node.js where the global is absent or experimental — connect() throws this error telling you to pass wsConstructor or bring your own ClientConnector.","triggerScenarios":"Calling the client factory with only { url, protocols } in Node.js (globalThis.WebSocket undefined on Node < 22, or behind a flag on early 22 builds), or in a bundled/pruned environment where the global was stripped.","commonSituations":"Using a browser-oriented WS client during SSR or in a bot/script; CI running an older Node image; test environment (jsdom) without a WebSocket polyfill; custom runtimes lacking the WHATWG WebSocket.","solutions":["Install the ws package and pass it: createClient({ url, wsConstructor: WebSocket }) with import WebSocket from 'ws'","Or run on Node 22+ where globalThis.WebSocket is available and enabled","Or supply a custom ClientConnector via the connector option to fully own the transport (also needed for binary frames, since the built-in connector is text-only)"],"exampleFix":"// before\nconst client = createClient({ url: 'ws://localhost:3000' }) // Node: Error: No WebSocket constructor is available...\n\n// after\nimport WebSocket from 'ws'\nconst client = createClient({ url: 'ws://localhost:3000', wsConstructor: WebSocket })","handlingStrategy":"validation","validationCode":"// Resolve a constructor before creating the client\nconst wsConstructor = globalThis.WebSocket\n  ?? (await import('ws')).default\nif (!wsConstructor)\n  throw new Error('No WebSocket implementation available in this runtime')\nconst client = createClient({ url, wsConstructor })","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass wsConstructor explicitly in Node entry points (import WebSocket from 'ws')","Pin a Node version policy and document the WebSocket requirement","For binary frames, bring a custom connector since the built-in one is text-only"],"tags":["websocket","nodejs","runtime-environment","client"],"backgroundTag":"websocket-undefined-in-node","analyzedSha":"677329427f32468c74b17f3ec47eeca4e05bec65","analyzedAt":"2026-08-18T17:29:58.153Z","contentChangedAt":"2026-08-18T17:29:58.153Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}