Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] global is not defined #1166

Closed
amitport opened this issue Oct 24, 2017 · 53 comments
Closed

[bug] global is not defined #1166

amitport opened this issue Oct 24, 2017 · 53 comments
Milestone

Comments

@amitport
Copy link
Contributor

amitport commented Oct 24, 2017

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

- [x] report a *bug*
- [ ] request a *feature*

Current behaviour

socket.io-client doesn't work with @angular/cli@1.5.0-rc.3 because of a dependency on node.js' global var
(seems to come from has-binary2@1.0.2, but I can't find who maintains that versions)

Steps to reproduce (if the current behaviour is a bug)

see angular/angular-cli#8160

Expected behaviour

Setup

  • OS:
  • browser:
  • socket.io version: 2.0.4
@amitport amitport changed the title global is not defined [bug] global is not defined Oct 24, 2017
@RicardoVaranda
Copy link

Also facing this issue currently

@darrachequesne
Copy link
Member

Since angular/angular-cli#8160 is now resolved, can we close this issue? Thanks!

@KevinGruber
Copy link

I don't think so, because in the angular/cli > 6 they permanently removed global.
Can you please start looking intoit again? angular/angular-cli#9827

BR,
Kevin

@mikkeldamm
Copy link

A workaround until Angular CLI maybe fixes it or make it possible to override the node command, then you can add the socket.io-client slim file in the paths section of the tsconfig.app.json

"paths": {
      ....
      "socket.io-client": ["../node_modules/socket.io-client/dist/socket.io.slim.js"]
}

@amitport
Copy link
Contributor Author

amitport commented Apr 8, 2018

Even if it does currently work, according to angular/angular-cli#9827 (comment), this will be an issue in Angular@6:

"[this] just generally make for a situation where browser code that shouldn't work at all works only when built in with very specific tooling. This is not a good situation. "

@ngervasi
Copy link

ngervasi commented Apr 23, 2018

I confirm it's broken in Angular 6 RC5. Does anybody know a workaround?

@mikkeldamm
Copy link

@ngervasi we have done it by doing like i commented here: #1166 (comment)

@ngervasi
Copy link

I tried:

{ "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "module": "es2015", "baseUrl": "", "types": [] }, "exclude": [ "test.ts", "**/*.spec.ts" ], "paths": { "socket.io-client": ["../node_modules/socket.io-client/dist/socket.io.slim.js"] } }

but it's not working in Angular 6RC5, I still get the error:

is-buffer.js:4 Uncaught ReferenceError: global is not defined at Object../node_modules/socket.io-client/node_modules/socket.io-parser/is-buffer.js (is-buffer.js:4) at __webpack_require__ (bootstrap:74) at Object../node_modules/socket.io-client/node_modules/socket.io-parser/binary.js (binary.js:8) at __webpack_require__ (bootstrap:74) at Object../node_modules/socket.io-client/node_modules/socket.io-parser/index.js (index.js:8) at __webpack_require__ (bootstrap:74) at Object../node_modules/socket.io-client/lib/index.js (index.js:7) at __webpack_require__ (bootstrap:74) at Object../src/app/_services/websocket.service.ts (log.service.ts:5) at __webpack_require__ (bootstrap:74)

@amitport
Copy link
Contributor Author

amitport commented May 3, 2018

According to angular/angular-cli#8160 (comment):

adding the following polyfills.ts should work in Angular@6:

// Add global to window, assigning the value of window itself.
(window as any).global = window;

In any case, this issue should not be resolved IMO, socket.io-client should either remove the implicit dependency on global, or document that a polyfill is needed (by anyone using the pre-bundled sources, not just Angular users)

(BTW where are the sources for latest has-binary2@1.0.2? maybe I could help)

@darrenmothersele
Copy link

Which is the better fix? #1166 (comment) (adding to paths in tsconfig) or #1166 (comment) (assigning to global)?

Both options worked for me in Angular v6 project generated with Angular CLI.

@darrachequesne
Copy link
Member

@amitport the sources for the has-binary2 package are here: https://github.com/darrachequesne/has-binary

Though I'm afraid it's not the sole occurence.

@amitport
Copy link
Contributor Author

amitport commented May 4, 2018

@darrenmothersele don't think there is exactly one "better". Adding 'global' may have side effects for other libs while adding the 'slim' file may have a negative effect on bundle size (not necessarily)

@darrachequesne added a PR darrachequesne/has-binary#4 (it's a start :))

@gentle9
Copy link

gentle9 commented May 5, 2018

I tried both solutions and with a current angular 6.0.0 project and none works. I added the paths entry to the tsconfig.app.json and the entry to polyfills.ts but still getting the same error in chrome.

@matheusdavidson
Copy link

matheusdavidson commented May 8, 2018

same here, the tsconfig solution does not work on ng cli 6

@reddyashok9
Copy link

reddyashok9 commented May 10, 2018

// Add global to window, assigning the value of window itself.
(window as any).global = window;

is working for me
Thanks.

@mandarini
Copy link

Works for me, too! Thanks!

@MarioCat
Copy link

It works for me too ! Thanks a lot ! :D

darrachequesne pushed a commit to darrachequesne/has-binary that referenced this issue May 14, 2018
@albertobasaglia
Copy link

where did you put (window as any).global = window; ?
Thanks.

@MarioCat
Copy link

MarioCat commented May 18, 2018 via email

@bibhas2
Copy link

bibhas2 commented May 31, 2018

where did you put...

Put this in your polyfills.ts

(window as any).global = window

@MarioCat
Copy link

MarioCat commented Jun 1, 2018 via email

@LucasFrecia
Copy link

@amitport thanks the workaround you provided works!! @angularteam, How come you keep breaking everything guys :/ you even managed to get your people in the rxjs team and suddenly even rxjs breaks everything now on version changes... been keeping up with angular since first stable release and Im amazed at your ability to break everything on each release.

@amitport
Copy link
Contributor Author

amitport commented Jul 13, 2018

@LucasFrecia it's a socket.io issue not Angular's. Angular did good by stopping to hide it in version 6 (which is marked as a major breaking release)

Added another pull request two weeks ago webmodules/blob#14 for https://github.com/webmodules/blob (used by engine.io-parser).

It isn't getting any response, anyone here can merge it? @darrachequesne @rauchg @nkzawa?

@JamesFoley1
Copy link

Adding the (window as any).global = window; to the polyfills.ts worked for me

@mohamedmezlini
Copy link

mohamedmezlini commented Jun 5, 2021

the polyfills.ts solution works for me
(window as any).global = window
#Angular_10

sunrise30 added a commit to sunrise30/socket.io-client that referenced this issue Jan 8, 2022
@locknono
Copy link

locknono commented Feb 7, 2022

@amitport So which version of socket.io-client could fix this problem? It seems that it does not work even after upgrading to version 2.3.0.

socket.io-client@2.3.0 still depends on engine.io-client@3.4.4, which references 'global', so it's getting the same error.

image
image

@amitport
Copy link
Contributor Author

amitport commented Feb 7, 2022

#1166 (comment)

I closed this issue as it started to diverge to many different issues related to global usage. This started as a specific use case and was not about removing 'global' in general. I suggest checking if your issue exists elsewhere and opening a new one if needed.

@locknono
Copy link

locknono commented Feb 8, 2022

@amitport So which version of socket.io-client could fix this problem? It seems that it does not work even after upgrading to version 2.3.0.

socket.io-client@2.3.0 still depends on engine.io-client@3.4.4, which references 'global', so it's getting the same error.

image image

I found it was actually an issue with vite and yarn pnp, instead of a sheer socket.io-client problem.
See vitejs/vite#1979

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests