walletconnect-monorepo
walletconnect-monorepo copied to clipboard
use Standalone Client Tips: Error: ws does not work in the browser. Browser clients must use the native WebSocket object
Error: ws does not work in the browser. Browser clients must use the native WebSocket object
Same error occured in production using vite react-ts
Same error occured in production using vite react-ts
error is global is not define
may edit vite.config.js
export default defineConfig({ ..., define: { global: "globalThis", }, });
and then add Buffer in main.ts
import { Buffer } from "buffer"; window.Buffer = Buffer;
error Error: ws does not work in the browser. Browser clients must use the native WebSocket object
at new module.exports (...\node_modules\@walletconnect\socket-transport\node_modules\ws\browser.js:4:1)
I cant fix it. Who know, how to do it?
Is this still an issue?
@finessevanes yes for vite projects (maybe not vite 4 tho). @marherb 's solution does work, but is a little heavy handed (i.e. the string "global" is replaced with "globalThis" so things like "global-warming" become "globalThis-warming".
A slightly more surgical approach is to use "global.WebSocket": "globalThis.WebSocket", instead of global: "globalThis"
@marherb if you want, edit your post if it works for you
@MacroChip what API are you using?
@finessevanes Did some diving because I am not using it directly. TLDR I think walletconnect v2 fixed this. I am also integrating Pera (linked above at https://github.com/perawallet/connect/issues/101) Only moderate confidence on the following: Pera connect ultimately gets to @walletconnect/http which seems to have disappeared in walletconnect v2. The spiritual successor is https://github.com/WalletConnect/walletconnect-utils/blob/24f59297429ee351d741d3fb84e558f0a1ede159/jsonrpc/ws-connection/src/ws.ts#L21 which, I believe, would not cause an error.
I’m closing this issue because it has been inactive for a few months.
Please reopen if you still encounter this issue with the latest version :)