Skip to content

[bug] global is not defined #1166

Closed
Closed

Description

@amitport

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

Activity

  1. changed the title [-]global is not defined[/-] [+][bug] global is not defined[/+] on Oct 24, 2017
  2. RicardoVaranda commented on Oct 24, 2017

    @RicardoVaranda

    Also facing this issue currently

  3. darrachequesne commented on Mar 10, 2018

    @darrachequesne
    Member

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

  4. KevinGruber commented on Mar 11, 2018

    @KevinGruber

    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

  5. mikkeldamm commented on Apr 6, 2018

    @mikkeldamm

    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"]
    }
    
  6. amitport commented on Apr 8, 2018

    @amitport
    ContributorAuthor

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

  7. ngervasi commented on Apr 23, 2018

    @ngervasi

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

  8. mikkeldamm commented on Apr 23, 2018

    @mikkeldamm

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

  9. ngervasi commented on Apr 23, 2018

    @ngervasi

    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)

  10. amitport commented on May 3, 2018

    @amitport
    ContributorAuthor

    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)

  11. darrenmothersele commented on May 4, 2018

    @darrenmothersele

    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.

  12. darrachequesne commented on May 4, 2018

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

  13. amitport commented on May 4, 2018

    @amitport
    ContributorAuthor

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

  14. gentle9 commented on May 5, 2018

    @gentle9

    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.

  15. matheusdavidson commented on May 8, 2018

    @matheusdavidson

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

  16. 37 remaining items

  17. Sliverb commented on Jan 25, 2019

    @Sliverb

    In my situation, it seems 2.2.0 depends on "engine.io-client": "~3.3.1", which depends on "engine.io-parser": "~2.1.1". However the engine.io-parser on my system seems to be 2.1.2 which still contains references to the 'global' variable. It seems this has been fixed in 2.1.3, but the npm i seems to be pulling 2.1.2

    If i had to make a guess, it seems it might be reverting back to 2.1.2 because the last build for 2.1.3 failed
    https://travis-ci.org/socketio/engine.io-parser

    Aside from that, i cant figure out why it keeps pulling the older version for me

  18. eldem commented on Feb 21, 2019

    @eldem

    I've same problem with Jest + React + Typescript, and couldn't fix it setting (window as any).global = window. I tried putting the code in a polyfills.ts file as well,

    setupFiles: [
        '<rootDir>/tst/polyfills.ts',
        '<rootDir>/tst/jest.setup.ts',
      ],
    

    and also using Jest's defineProperty method, but still no luck.

    Object.defineProperty((window as any), 'global', {});
    
    copyProps(window, global);
    

    Anyone able to solve this for Jest, or know how I can make this work? @amitport any idea?

    Thanks,

  19. amitport commented on Feb 21, 2019

    @amitport
    ContributorAuthor

    @eldem and others. I'm closing this as this thread became too a generic mix of possibly unrelated issues. If anybody sees a problem relating to global and socket.io-client, please open a new issue with reproduction instructions, errors shown in console, versions used, etc.,.

  20. dovk commented on Mar 1, 2019

    @dovk

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

  21. eldem commented on Mar 1, 2019

    @eldem

    For my question above: Babel was causing the error while transforming the socket.io file. Adding it in the transformIgnore worked for me.

    transformIgnorePatterns: [
        '<rootDir>/node_modules/../lib/socket.io.js'
      ]
    
  22. yoann-dh commented on May 30, 2019

    @yoann-dh

    @amitport methode work perfectly

  23. mohamedmezlini commented on Jun 5, 2021

    @mohamedmezlini

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

  24. locknono commented on Feb 7, 2022

    @locknono

    @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

  25. amitport commented on Feb 7, 2022

    @amitport
    ContributorAuthor

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

  26. locknono commented on Feb 8, 2022

    @locknono

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions