Starting with v22.4.0
, Node.js has a native WebSocket implementation. If you are using an older version of Node.js, upgrade your Node.js version.
If you are unable to upgrade your Node.js version, do the following:
Install the ws
package:
npm install ws
At the top of your code, (or anywhere before calling your first Client.connect
), add the following code:
ES6/Import:
import { WebSocket } from "ws";
globalThis.WebSocket = WebSocket;
CommonJS:
const { WebSocket } = require("ws");
globalThis.WebSocket = WebSocket;