Angular-cli: ReferenceError: Can't find variable: global

Created on 12 May 2018  路  7Comments  路  Source: angular/angular-cli

Versions

Angular CLI: 6.0.1
Node: 8.11.1
OS: darwin x64
Angular: 6.0.1
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.1
@angular-devkit/build-angular     0.6.1
@angular-devkit/build-optimizer   0.6.1
@angular-devkit/core              0.6.1
@angular-devkit/schematics        0.6.1
@ngtools/webpack                  6.0.1
@schematics/angular               0.6.1
@schematics/update                0.6.1
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.6.0

node version: v8.11.1
npm version: 6.0.0

macOS Sierra

Repro steps

  • Install socket.io-client
  • in app.component.ts, import socket.io-client as io, and do io('...')

Observed behavior

We get this error in the browser: ReferenceError: Can't find variable: global

Desired behavior

No error

Mention any other details that might be useful (optional)

A workaround fixed the issue:

  • Workaround 1: add (windows as any).global = window to polyfills.ts
  • Workaround 2: add window.global = window to index.html
devkibuild-angular

Most helpful comment

Well, the issue is that the library tries to find a variable called global which is expected to contain all existing global variables (which usually window does). But this variable is for some reasons undefined so you need to define it on your own by either adding it in the _index.html_ file by adding a script tag to the head:

<script type="text/javascript">
window.global = window;
</script>

Or you do the same thing in _polyfills.ts_ with some decorations to make the Typescript compiler happy:

(windows as any).global = window;

All 7 comments

+1
I have the same issue. The workaround fixes the issue though...

Hello Everyone.
Just facing the issue... I did not really understood the workaround. Where to set the variables please?
I thank you in advance for your help.

Well, the issue is that the library tries to find a variable called global which is expected to contain all existing global variables (which usually window does). But this variable is for some reasons undefined so you need to define it on your own by either adding it in the _index.html_ file by adding a script tag to the head:

<script type="text/javascript">
window.global = window;
</script>

Or you do the same thing in _polyfills.ts_ with some decorations to make the Typescript compiler happy:

(windows as any).global = window;

Thanks. I just modified my index.html.
Works fine.

I have the same issue caused by intl polyfill. Workaround 2 worked fine.

In Angular CLI 6 we did remove the node global and others. You can read more about this in https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814.

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings