socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

[bug] global is not defined

Open amitport opened this issue 9 years ago • 53 comments

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/[email protected] because of a dependency on node.js' global var (seems to come from [email protected], but I can't find who maintains that versions)

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

see https://github.com/angular/angular-cli/issues/8160

Expected behaviour

Setup

  • OS:
  • browser:
  • socket.io version: 2.0.4

amitport avatar Oct 24 '17 07:10 amitport

Also facing this issue currently

RicardoVaranda avatar Oct 24 '17 14:10 RicardoVaranda

Since https://github.com/angular/angular-cli/issues/8160 is now resolved, can we close this issue? Thanks!

darrachequesne avatar Mar 10 '18 08:03 darrachequesne

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

BR, Kevin

KevinGruber avatar Mar 11 '18 09:03 KevinGruber

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"]
}

mikkeldamm avatar Apr 06 '18 11:04 mikkeldamm

Even if it does currently work, according to https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814, 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. "

amitport avatar Apr 08 '18 06:04 amitport

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

ngervasi avatar Apr 23 '18 10:04 ngervasi

@ngervasi we have done it by doing like i commented here: https://github.com/socketio/socket.io-client/issues/1166#issuecomment-379225639

mikkeldamm avatar Apr 23 '18 10:04 mikkeldamm

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)

ngervasi avatar Apr 23 '18 13:04 ngervasi

According to https://github.com/angular/angular-cli/issues/8160#issuecomment-386153833:

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 [email protected]? maybe I could help)

amitport avatar May 03 '18 05:05 amitport

Which is the better fix? https://github.com/socketio/socket.io-client/issues/1166#issuecomment-379225639 (adding to paths in tsconfig) or https://github.com/socketio/socket.io-client/issues/1166#issuecomment-386195105 (assigning to global)?

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

darrenmothersele avatar May 04 '18 11:05 darrenmothersele

@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.

darrachequesne avatar May 04 '18 12:05 darrachequesne

@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 https://github.com/darrachequesne/has-binary/pull/4 (it's a start :))

amitport avatar May 04 '18 13:05 amitport

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.

gentle9 avatar May 05 '18 00:05 gentle9

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

matheusdavidson avatar May 08 '18 15:05 matheusdavidson

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

is working for me Thanks.

reddyashok9 avatar May 10 '18 11:05 reddyashok9

Works for me, too! Thanks!

mandarini avatar May 10 '18 14:05 mandarini

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

MarioCat avatar May 10 '18 17:05 MarioCat

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

albertobasaglia avatar May 17 '18 19:05 albertobasaglia

Hello,

Just right there (on the picture).

2018-05-17 21:07 GMT+02:00 Alberto Basaglia [email protected]:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/socketio/socket.io-client/issues/1166#issuecomment-389975067, or mute the thread https://github.com/notifications/unsubscribe-auth/APZTlGpMwMpnAscifHT2ZQrRUSJqLejCks5tzcp3gaJpZM4QD_kq .

--

Arnaud Tussy-Vassilieff Développeur web full-stack chez Leaf Websites

Site web : www.leaf-website.esy.es http://www.leaf-website.esy.es Mail : [email protected] [email protected] Tél : 07 77 83 29 44

MarioCat avatar May 18 '18 19:05 MarioCat

where did you put...

Put this in your polyfills.ts

(window as any).global = window

bibhas2 avatar May 31 '18 16:05 bibhas2