SocketCluster / socketcluster-client

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

global is not defined in Angular 6 #118

Closed DanielRose closed 1 year ago

DanielRose commented 6 years ago

I am using this in an Angular app. After upgrading to Angular 6, I get the following error:

sctransport.js:7 Uncaught ReferenceError: global is not defined at Object../node_modules/socketcluster-client/lib/sctransport.js (sctransport.js:7) at webpack_require__ (bootstrap:81) at Object../node_modules/socketcluster-client/lib/scclientsocket.js (scclientsocket.js:6) at webpack_require (bootstrap:81) at Object../node_modules/socketcluster-client/index.js (index.js:1) at __webpack_require (bootstrap:81) at Object../src/app/SocketClusterClient.ts (main.js:916)

The SocketClusterClient.ts is my code, and includes

import * as sc from 'socketcluster-client';

The reason is that Angular no longer includes shims for node globals. As a workaround I can manually shim it, but it would be better to solve this here.

seiyria commented 5 years ago

For those wondering, the shim is (<any>window).global = window. Simple enough, but should not be necessary.

minsulander commented 1 year ago

Same issue in a newly created Vue/Vite project with socketcluster-client 17.1.1. Easily worked around by adding define: { global: {} } in vite.config.ts, but a bit annoying...

This line seems to be the culprit: https://github.com/SocketCluster/socketcluster-client/blob/f36d89e9f9ac16a0c8dcc31f9dfdf0a5f070d1e0/lib/transport.js#L6

jondubois commented 1 year ago

Should be fixed now without the need for a shim though I didn't test with any specific framework . Feel free to reopen if needed.

minsulander commented 1 year ago

@jondubois thanks, that was quick! :) Unfortunately on Vue/Vite without the global shim I now get the same error from this line: https://github.com/SocketCluster/socketcluster-client/blob/69fb145a62672b584272710cf991d1b684b98e22/lib/clientsocket.js#L176

Replacing global with window in clientsocket.js worked for me...

jondubois commented 1 year ago

@minsulander OK. I pushed another update socketcluster-client@17.2.2 just now which should fix this issue as well. It's probably best to upgrade as there were other parts in the code which had global references which could affect edge cases. Let me know if there is any other issue.

minsulander commented 1 year ago

Well done @jondubois, works like a charm now with 17.2.2. Thanks a bunch!