socket.io-client: [bug] global is not defined

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 https://github.com/angular/angular-cli/issues/8160

Expected behaviour

Setup

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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 28
  • Comments: 53 (15 by maintainers)

Commits related to this issue

Most upvoted comments

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 has-binary2@1.0.2? maybe I could help)

where did you put…

Put this in your polyfills.ts

(window as any).global = window

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

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

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)

The (window as any).global = window; in pollyfills.ts workaround seems to work for me in angular core 6.1.0. Let’s hope socket.io will update soon…

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

is working for me Thanks.

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

Working with Ionic4/Angular7 - installing 2.1.1 and adding (window as any).global = window in \src\polyfills.ts worked - Thank you!

@darrachequesne - done, I’ve submitted a request to NPM

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

@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 https://github.com/webmodules/blob/pull/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?

Works for me, too! Thanks!

@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 😃)

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

@alimek rephrase: if your bug is the same as this one, add details. Otherwise please open a new issue (which may be a regression).

(BTW, I’m not a maintainer of this lib, I’m just commenting about how issue handling works in general. If you want your issue addressed you need to add actionable details and post it appropriately)

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

between line 63 and 70

2018-05-31 18:31 GMT+02:00 Bibhas Bhattacharya notifications@github.com:

where did you put…

Put this in your polyfills.ts

(window as any).global = window

— 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-393592879, or mute the thread https://github.com/notifications/unsubscribe-auth/APZTlPci9GKE3R8lwc1g3qfqWODeXIPiks5t4Br1gaJpZM4QD_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 : a.tussy.vassilieff@gmail.com a.tussy.vassilieff@gmail.com Tél : 07 77 83 29 44

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