walletconnect-monorepo: this.events.off is not a function error if Node polyfill
Issue
@walletconnect/web3wallet throws an error this.events.off is not a function when pairing a session when Node libraries are polyfilled for local development in vite based project.
This issue started to occur in recent releases, and my current workaround is to stick to version v1.9.1.
Notes
This is a follow-up to issue #3913, which you closed, requesting a minimal sample repo to replicate this issue. I am providing it here.
Versions
- Client: SvelteKit and Vite
- Version v1.9.5
Reproduction
Steps to reproduce the behavior:
- git clone https://github.com/peterpeterparker/web3wallet-issue-3913
- cd web3wallet-issue-3913
- npm ci
- npm run dev
Try to establish a connection for example with https://app.uniswap.org/swap?chain=goerli
Expected behavior
Being able to pair a session without errors.
Screenshots
About this issue
- Original URL
- State: open
- Created 6 months ago
- Comments: 18 (2 by maintainers)
Commits related to this issue
- Use different plugin for polyfilling see https://github.com/WalletConnect/walletconnect-monorepo/issues/4064#issuecomment-1885607272 — committed to pendulum-chain/portal by ebma 5 months ago
- Use different plugin for polyfilling see https://github.com/WalletConnect/walletconnect-monorepo/issues/4064#issuecomment-1885607272 — committed to pendulum-chain/portal by ebma 5 months ago
- Use different plugin for polyfilling / Fix walletconnect (#332) * Use different plugin for polyfilling see https://github.com/WalletConnect/walletconnect-monorepo/issues/4064#issuecomment-1885607272... — committed to pendulum-chain/portal by ebma 5 months ago
Hey everybody, we encountered a similar issue today and this is what we learned:
The
JsonRpcProviderclass uses theeventspackage https://github.com/WalletConnect/walletconnect-utils/blob/ff9e57adcd08b1610f13eba659def97dcf21bb83/jsonrpc/provider/src/provider.ts#L1eventspackage is a built in package in a Node.js environment, but not in the browser. There is a polyfill available that is a dependency of@walletconnect/core: https://github.com/WalletConnect/walletconnect-monorepo/blob/82298c93e31b8b05ee190cd23f0aab61bdded12a/packages/core/package.json#L47@walletconnect/jsonrpc-provider(and probably other walletconnect packages) does not have theeventspolyfill in its dependencies: https://github.com/WalletConnect/walletconnect-utils/blob/ff9e57adcd08b1610f13eba659def97dcf21bb83/jsonrpc/provider/package.json#L48-L51Our project uses another package (
aws-sdk) that requires an old version of theeventspolyfill (this version does not have theEventEmitter.prototype.offfunction): https://github.com/aws/aws-sdk-js/blob/fd95d9fe09aaef540f6a50763e385506d4c9abb6/package.json#L45Because
@walletconnect/jsonrpc-providerdoe not have theeventspackage in its dependencies, webpack resolves the wrong version of theeventspackage during build (the one required byaws-sdk). Because of the missingEventEmitter.prototype.offin this version, the following line will throw an error: https://github.com/WalletConnect/walletconnect-utils/blob/ff9e57adcd08b1610f13eba659def97dcf21bb83/jsonrpc/provider/src/provider.ts#L47If this interpretion is right, the problem can be solved by adding the required version of the
eventspackage to the package.json of@walletconnect/jsonrpc-provider. The same might be needed for other packages with the same missing dependency (eg.@walletconnect/heartbeat).If you are using pnpm, you can patch the package.json of these packages like in the following snippet. Otherwise I think you need to wait for a release with adjusted dependencies or configure how your bundler resolves the
eventspackage.Thanks for the feedback @glitch-txs !
Indeed I think polyfilling NodeJS with vite / esbuild seems to override some global methods used by the SDK.
I removed the plugin which “resolved” the issue but, I just noticed that I may need to polyfill in some other cases. So I’m still interested for a solution.
I cannot reopen the issue, only option provided by GitHub is “comment”.
So, can you reopen it?
Issue closed without without any comments or references? Was it solved or it won’t be solved? Any PR, references or commit to share?
Not really, @adrianotadao. I opened the issue, documented it, and provided a sample repo, but I did not debug the WalletConnect code.
That said, in our app, after bumping a few dependencies, I actually noticed that we weren’t requiring the usage of the polyfier plugin (
@esbuild-plugins/node-modules-polyfill) that leads to the issue. So, I removed the plugin from our vite config, and, by extension, it resolved the issue I was facing.