Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: global is not defined - Intl polyfill #9920

Closed
MattMorrisDev opened this issue Mar 9, 2018 · 12 comments
Closed

ReferenceError: global is not defined - Intl polyfill #9920

MattMorrisDev opened this issue Mar 9, 2018 · 12 comments

Comments

@MattMorrisDev
Copy link

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:
    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.

@clydin
Copy link
Member

clydin commented Mar 9, 2018

(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: #9827 (comment)

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

@clydin clydin closed this as completed Mar 9, 2018
@shiamalon
Copy link

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.

@MattMorrisDev
Copy link
Author

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

@takahser
Copy link

takahser commented May 31, 2018

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: auth0/auth0.js#753

@MateWW
Copy link

MateWW commented Jun 7, 2018

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

@ghost
Copy link

ghost commented Jul 17, 2018

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

Fixed it by adding

import 'intl';

to polyfills.ts

@shiamalon
Copy link

@MattMorrisDev thank's!

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

@Sharpiro
Copy link

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

@aniruddhadas9
Copy link
Contributor

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



sfeilmeier added a commit to sfeilmeier/openems that referenced this issue Dec 11, 2018
sfeilmeier added a commit to OpenEMS/openems that referenced this issue Jan 15, 2019
* Migrate the complete (websocket-)communication to JSON-RPC. See the JSON-RPC specification on  https://www.jsonrpc.org/specification. 
* The base classes for JSON-RPC are in the package io.openems.common/jsonrpc (https://github.com/OpenEMS/openems/blob/develop/io.openems.common/src/io/openems/common/jsonrpc)
* Added some documentation on how the JSON-RPC communication works: https://openems.io/openems/latest/component-communication/index.adoc (https://github.com/OpenEMS/openems/blob/develop/doc/modules/ROOT/pages/component-communication) using antora + asciidoc + mermaid.js

To implement a new JSON-RPC Request/Notification from UI via Backend to Edge, touch the following places
- Add the Request in UI (https://github.com/OpenEMS/openems/blob/develop/ui/src/app/shared/jsonrpc/request)
- Send the Request from UI (https://github.com/OpenEMS/openems/blob/develop/ui/src/app/shared/edge/edge.ts#L146)
- Handle the Request in Backend (https://github.com/OpenEMS/openems/blob/develop/io.openems.backend.uiwebsocket.impl/src/io/openems/backend/uiwebsocket/impl/OnRequest.java#L101)
- Handle the Request in Edge (https://github.com/OpenEMS/openems/blob/develop/io.openems.edge.controller.api.websocket/src/io/openems/edge/controller/api/websocket/OnRequest.java#L91)

Plus many other changes:

Common:

* Reimplement AbstractWebsocketClient and -Server
* Implement fully asynchronous handling with functional interfaces
* Pin JSON dependency version for Apache Felix
* Add OpenemsNamedException to get unique IDs for OpenEMS errors
* Set EVCS Controller Charge-Mode from UI Widget
* Add EVCS Widgets to Index & Historic view
* Apply Checkstyle to many projects
* Improve Javadoc

Backend:

* Improve Metadata api & services
* Improve Timedata api & services
* Add io.openems.backend.b2bwebsocket module (e.g. SetGridConnSchedule and GetStatusOfEdges)
* Create Backend Timedata.Dummy service
* Define a EdgeConfig format. Parsing XML meta information from Bundle.
* Add AbstractOpenemsBackendComponent parent class; similar to AbstractOpenemsComponent in Edge
* Change format for Metadata.File

Edge:

* Refactor AbstractOpenemsComponent.activate()-method. No need for properties, as they are available via ComponentContext
- Get rid of mandatory 'service_pid' in every Component Config.
- Make Factory-PID available
* Create Symmetric Balancing Schedule Controller für "SetGridConnSchedule": takes a schedule with ActivePowerSetPoints for defined timestamps and periods
* Modbus-TCP slave: add automatic retry for starting the server
* Fix REST-Api: use ApiWorker to constantly write values till timeout
* EVCS-Api: add ChargePower channel
* Add EVCS simulator
* ComponentManager is able to update a component configuration using UpdateComponentConfig JSON-RPC Request
* Split 'Sum' in 'Sum' and 'SumImpl'
* Trigger OsgiValidateWorker on every configuration change
* Fix KEBA using value instead of writeValue
* Add EVCS Controller
* Add Equals/LessThan/GreaterThan Channels for ManagedSymmetricEss and ManagedAsymmetricEss
* Add OneFullCycleController

UI
* Refactor Chart Sections
* UI: Refactor UI language selection
* Remove temporarily disabled parts of UI
* Fix formatting of numbers using 'formatNumber'
* avoid having 'Edge' and 'Config' as @input parameter; use Service instead
* Remove obsolete 'intl' polyfill (angular/angular-cli#9920)
* Update @ionic/angular
@magervino
Copy link

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! =)

@danieldanielecki
Copy link

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.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants