Angular-cli: ReferenceError: global is not defined - Intl polyfill

Created on 9 Mar 2018  路  12Comments  路  Source: angular/angular-cli

Versions

Angular CLI: 6.0.0-beta.5
Node: 8.9.0
OS: darwin x64
Angular: 6.0.0-beta.7
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cdk: 6.0.0-beta.4
@angular/cli: 6.0.0-beta.5
@angular/material: 6.0.0-beta.4
@angular-devkit/build-optimizer: 0.4.5
@angular-devkit/core: 0.4.5
@angular-devkit/schematics: 0.4.5
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 6.0.0-beta.5
@schematics/angular: 0.4.5
@schematics/package-update: 0.4.5
typescript: 2.6.2
webpack-bundle-analyzer: 2.10.0
webpack: 4.0.1

(Side note: the issue template says to use ng --version, but that was removed in a recent version and wasn't mentioned as a breaking change :))

Repro steps

  • I'm importing the following in polyfills.ts:
    ts import 'intl'; import 'intl/locale-data/jsonp/en';

Observed behavior

On app startup, I see the following error:

index.js:2 Uncaught ReferenceError: global is not defined
    at eval (index.js:2)
    at Object../node_modules/intl/index.js (polyfills.js:2880)
    at __webpack_require__ (runtime.js:75)
    at eval (polyfills.ts:36)
    at Object../src/polyfills.ts (polyfills.js:2926)
    at __webpack_require__ (runtime.js:75)
    at Object.1 (polyfills.js:2937)
    at __webpack_require__ (runtime.js:75)
    at checkDeferredModules (runtime.js:44)
    at Array.webpackJsonpCallback [as push] (runtime.js:31)

where index.js is the following:

// Expose `IntlPolyfill` as global to add locale data into runtime later on.
global.IntlPolyfill = require('./lib/core.js');

// Require all locale data for `Intl`. This module will be
// ignored when bundling for the browser with Browserify/Webpack.
require('./locale-data/complete.js');

// hack to export the polyfill as global Intl if needed
if (!global.Intl) {
    global.Intl = global.IntlPolyfill;
    global.IntlPolyfill.__applyLocaleSensitivePrototypes();
}

// providing an idiomatic api for the nodejs version of this module
module.exports = global.IntlPolyfill;

It doesn't crash the application, although the browser I'm using doesn't need the polyfill. But I wouldn't expect to see this error normally.

Most helpful comment

for those, who get the slightly different error ReferenceError: global is not defined at Object.getWindow, this is the fix:

add to polyfill.ts:
(window as any).global = window;

see: https://github.com/auth0/auth0.js/issues/753

All 12 comments

(Side note: the issue template says to use ng --version, but that was removed in a recent version and wasn't mentioned as a breaking change :))

^^^ That was a bug and is fixed in master

As to the issue, the value global does not exist in a web browser. Node shims have been removed in 6.0. For more details on why, please see this comment: https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814

The polyfill in question is also no longer needed by Angular as mentioned in the changelog for 5.0: https://github.com/angular/angular/blob/master/CHANGELOG.md#500-pentagonal-donut-2017-11-01

I'm having the exact same issue after upgrading my project to angular 6. Is there a solution for it? I'm not really understanding how to solve it from the reply above.

@shiamalon Remove the intl imports completely - they aren't needed in v6.

for those, who get the slightly different error ReferenceError: global is not defined at Object.getWindow, this is the fix:

add to polyfill.ts:
(window as any).global = window;

see: https://github.com/auth0/auth0.js/issues/753

For me polyfills.js from project generated by latest cli solved problem :)

Had the same issue after upgrading to angular 6 with @angular/cli ng update

Fixed it by adding

import 'intl';

to polyfills.ts

@MattMorrisDev thank's!

@shiamalon Remove the intl imports completely - they aren't needed in v6.

I just had to use @takahser 's fix using a fresh install of angular cli 6.1.0-rc.3 in order to use Buffer

I am even getting this in router transiction when moved from angular 5 to 7.0.0.beta.5 . Any one got this issue before

聽 | defaultErrorLogger | @ | core.js:11804
-- | -- | -- | --
聽 | push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError | @ | core.js:11850
聽 | next | @ | core.js:13818
聽 | schedulerFn | @ | core.js:9168
聽 | push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub | @ | Subscriber.js:209
聽 | push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next | @ | Subscriber.js:147
聽 | push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next | @ | Subscriber.js:80
聽 | push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next | @ | Subscriber.js:55
聽 | push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next | @ | Subject.js:47
聽 | push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit | @ | core.js:9152
聽 | (anonymous) | @ | core.js:13376
聽 | push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke | @ | zone.js:388
聽 | push../node_modules/zone.js/dist/zone.js.Zone.run | @ | zone.js:138
聽 | push../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular | @ | core.js:13313
聽 | onHandleError | @ | core.js:13376
聽 | push../node_modules/zone.js/dist/zone.js.ZoneDelegate.handleError | @ | zone.js:392
聽 | push../node_modules/zone.js/dist/zone.js.Zone.runGuarded | @ | zone.js:154
聽 | _loop_1 | @ | zone.js:677
聽 | api.microtaskDrainDone | @ | zone.js:686
聽 | drainMicroTaskQueue | @ | zone.js:602
聽 | Promise.then (async) | 聽 | 聽
聽 | scheduleMicroTask | @ | zone.js:578
聽 | push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask | @ | zone.js:410
聽 | onScheduleTask | @ | zone.js:297
聽 | push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask | @ | zone.js:401
聽 | push../node_modules/zone.js/dist/zone.js.Zone.scheduleTask | @ | zone.js:232
聽 | push../node_modules/zone.js/dist/zone.js.Zone.scheduleMicroTask | @ | zone.js:252
聽 | scheduleResolveOrReject | @ | zone.js:862
聽 | resolvePromise | @ | zone.js:808
聽 | (anonymous) | @ | zone.js:724
聽 | webpackJsonpCallback | @ | bootstrap:25



what fixed for me: npm install --save-dev intl.
I guess what broke it here is that i installed testing frameworks through angular cli and then changed the app file.
Now everything up and running! =)

In case, if your target is node in webpack (target: 'node'), because you want to fix "Can't resolve 'fs'. Then you're getting the following error Fix: "Uncaught ReferenceError: global is not defined" do it as follows node: { global: true, fs: 'empty' }. Bonus: if you got error "Uncaught ReferenceError: exports is not defined". simply add libraryTarget: 'umd'. The complete webpack config code is below.

const webpackConfig = {
  node: { global: true, fs: 'empty' }, // Fix: "Uncaught ReferenceError: global is not defined", and "Can't resolve 'fs'".
  output: {
    libraryTarget: 'umd' // Fix: "Uncaught ReferenceError: exports is not defined".
  }
};

module.exports = webpackConfig; // Export all custom Webpack configs.

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

Related issues

hareeshav picture hareeshav  路  3Comments

gotschmarcel picture gotschmarcel  路  3Comments

JanStureNielsen picture JanStureNielsen  路  3Comments

delasteve picture delasteve  路  3Comments

rajjejosefsson picture rajjejosefsson  路  3Comments