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:

  1. git clone https://github.com/peterpeterparker/web3wallet-issue-3913
  2. cd web3wallet-issue-3913
  3. npm ci
  4. 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

error

About this issue

  • Original URL
  • State: open
  • Created 6 months ago
  • Comments: 18 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Hey everybody, we encountered a similar issue today and this is what we learned:

  1. The JsonRpcProvider class uses the events package https://github.com/WalletConnect/walletconnect-utils/blob/ff9e57adcd08b1610f13eba659def97dcf21bb83/jsonrpc/provider/src/provider.ts#L1

  2. events package 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

  3. @walletconnect/jsonrpc-provider (and probably other walletconnect packages) does not have the events polyfill in its dependencies: https://github.com/WalletConnect/walletconnect-utils/blob/ff9e57adcd08b1610f13eba659def97dcf21bb83/jsonrpc/provider/package.json#L48-L51

  4. Our project uses another package (aws-sdk) that requires an old version of the events polyfill (this version does not have the EventEmitter.prototype.off function): https://github.com/aws/aws-sdk-js/blob/fd95d9fe09aaef540f6a50763e385506d4c9abb6/package.json#L45

  5. Because @walletconnect/jsonrpc-provider doe not have the events package in its dependencies, webpack resolves the wrong version of the events package during build (the one required by aws-sdk). Because of the missing EventEmitter.prototype.off in this version, the following line will throw an error: https://github.com/WalletConnect/walletconnect-utils/blob/ff9e57adcd08b1610f13eba659def97dcf21bb83/jsonrpc/provider/src/provider.ts#L47

If this interpretion is right, the problem can be solved by adding the required version of the events package 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 events package.

"pnpm": {
  "packageExtensions": {
    "@walletconnect/jsonrpc-provider": {
      "dependencies": {
        "events": "^3.3.0"
      }
    }
  }
},

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.