ReferenceError: global is not defined with Stream and Angular 7.1
Asked Answered
T

1

9

I'm trying to use Stream with Angular 7, however I am getting the following error.

ReferenceError: global is not defined ReferenceError: global is not defined at Object../node_modules/faye/src/util/browser/event.js (event.js:45) at webpack_require (bootstrap:83) at Object../node_modules/faye/src/protocol/client.js (client.js:8) at webpack_require (bootstrap:83) at Object../node_modules/faye/src/faye_browser.js (faye_browser.js:9) at webpack_require (bootstrap:83) at Object../node_modules/getstream/lib/lib/client.js (client.js:25) at webpack_require (bootstrap:83) at Object../node_modules/getstream/lib/getstream.js (getstream.js:6) at webpack_require (bootstrap:83) at resolvePromise (zone.js:814) at resolvePromise (zone.js:771) at zone.js:873 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:16147) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188) at drainMicroTaskQueue (zone.js:595)

I am using the package "getstream" from npm version 4.1.0.

Here is a snippet of code and how I am referencing it.

import { Injectable } from '@angular/core';
import { Constants } from './../constants';
import * as stream from 'getstream';

@Injectable({
  providedIn: 'root'
})
export class ActivityService {
  userToken: string;
  streamClient: stream.Client;

  constructor(public azureService: AzureService) { 
    console.log("Activity Service");
  }

  async initializeClient() {
    try {
      await this.getUserToken();

      if (this.userToken) {
        this.streamClient = await stream.connect(Constants.streamKey, this.userToken, Constants.streamAppId);
      }
    }
    catch (error) {
      console.log('Error creating stream client - ', error);
      throw error;
    }
  }

I've tried declaring global but I am not sure what else to try. What's the proper way to import and use Stream in this case?

Twinflower answered 15/1, 2019 at 15:28 Comment(0)
V
24

This looks like an Angular issue. They do offer some workarounds for this. https://github.com/angular/angular-cli/issues/8160 https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063

(window as any).global = window;
Vesting answered 16/1, 2019 at 10:21 Comment(2)
Thanks adding that line to my polyfills.ts resolved the problem.Twinflower
Thanks, guys. saved the day in minutesZacynthus

© 2022 - 2024 — McMap. All rights reserved.