SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
290 stars 92 forks source link

SyntaxError: Identifier 'global' has already been declared #153

Open johnny-mcfadden-dailypay opened 1 week ago

johnny-mcfadden-dailypay commented 1 week ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch socketcluster-client@19.1.2 for the project I'm working on ("expo": "51.0.17" + "react-native": "0.74.2" being the main upgrade drivers that resulted in this issue).

On web, we are running into an issue with global already declared, causing our web bundling to fail.

Screenshot 2024-07-01 at 14 28 46

Here is the diff that solved my problem that I hoped to shared with others.

diff --git a/node_modules/socketcluster-client/lib/ws-browser.js b/node_modules/socketcluster-client/lib/ws-browser.js
index 32e279c..1614e0d 100644
--- a/node_modules/socketcluster-client/lib/ws-browser.js
+++ b/node_modules/socketcluster-client/lib/ws-browser.js
@@ -1,11 +1,11 @@
-let global;
+let globalScope;
 if (typeof WorkerGlobalScope !== 'undefined') {
-  global = self;
+  globalScope = self;
 } else {
-  global = typeof window !== 'undefined' && window || (function() { return this; })();
+  globalScope = typeof window !== 'undefined' && window || (function() { return this; })();
 }

-const WebSocket = global.WebSocket || global.MozWebSocket;
+const WebSocket = globalScope.WebSocket || globalScope.MozWebSocket;

 /**
  * WebSocket constructor.

This issue body was partially generated by patch-package.