Upgrading to angular-6.x gives "Uncaught ReferenceError: global is not defined"

Solution 1:

Adding this line to polyfills.ts should resolve node global error

(window as any).global = window;

The solution was mentioned in this angular-cli issue thred

Solution 2:

Add following code in your starting page e.g. index.html

var global = global || window;
var Buffer = Buffer || [];
var process = process || {
  env: { DEBUG: undefined },
  version: []
};

Example:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Client</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script>
    var global = global || window;
    var Buffer = Buffer || [];
    var process = process || {
      env: { DEBUG: undefined },
      version: []
    };
  </script>
</head>
<body>
  <app-root>
    <div class="loader"></div>
  </app-root>
</body>
</html>

Above will work on hybrid app (in Node environment) as well as in browser

  • for "Uncaught ReferenceError: global is not defined":

    var global = global || window;
    
  • for "Uncaught ReferenceError: Buffer is not defined":

    var Buffer = Buffer || [];
    
  • for "Uncaught ReferenceError: process is not defined":

    var process = process || {
      env: { DEBUG: undefined }
    }
    
  • for "Uncaught TypeError: Cannot read property 'slice' of undefined":

    var process = process || {
      env: { DEBUG: undefined },
      version: []
    };
    

Solution 3:

I use HttpClient and accidentally selected the default import which was 'selenium-webdriver/http'

If your app.module.ts has import { HttpClient } from 'selenium-webdriver/http';

Update it to import { HttpClient } from '@angular/common/http';

That fixed my issue.