socket.io-client: Library socket.io-client is not compatible with latest Angular 6

I want to:

  • report a bug - solved, look last two lines for workaround

Library socket.io-client is not compatible with latest Angular 6. There is error at execution time:

ReferenceError: global is not defined at Object…/node_modules/socket.io-client/node_modules/socket.io-parser/is-buffer.js (is-buffer.js:4)

Please read comment below to know more: https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814

If you want to see this error for yourself, add to angular 6.0 app simple service like that below:

import { Injectable } from 'angular/core';
import * as socketIo from 'socket.io-client';

@Injectable()
export class SocketService {
    private socket;

    constructor() {}

    public initSocket(): void {
        this.socket = socketIo();
    }
}

Error disappears when you remove initSocket function.



Ok, there is workaround in Angular 6 to work with socket.io-client. Add (window as any).global = window; to polyfills.ts

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 42
  • Comments: 16 (3 by maintainers)

Most upvoted comments

mine started working by simply adding the following

Inside polyfills.ts add:

(window as any).global = window;

angular 6.1.0 socket.io-client 2.1.1

This continues to work after upgrading to angular 7.1.x

@realshaft - the above work-around worked for me in Ionic 4 / Angular 6 web app

Ok, there is workaround in Angular 6 to work with socket.io-client. Add (window as any).global = window; to polyfills.ts (located in src folder)

(window as any).global = window; This worked for me as well on Angular 7.