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

[bug] Uncaught ReferenceError: global is not defined #8160

Closed
amitport opened this issue Oct 24, 2017 · 37 comments · Fixed by #8250
Closed

[bug] Uncaught ReferenceError: global is not defined #8160

amitport opened this issue Oct 24, 2017 · 37 comments · Fixed by #8250
Assignees
Labels
needs: investigation Requires some digging to determine if action is needed P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful type: bug/fix

Comments

@amitport
Copy link

amitport commented Oct 24, 2017

Bug Report or Feature Request (mark with an x)

- [X] bug report -> please search issues before submitting
- [ ] feature request

Versions.

Angular CLI: 1.5.0-rc.3
Node: 8.7.0
OS: win32 x64
Angular: 4.4.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, tsc-wrapped, upgrade

@angular/cdk: 2.0.0-beta.12
@angular/cli: 1.5.0-rc.3
@angular/flex-layout: 2.0.0-beta.9
@angular/material: 2.0.0-beta.12
@angular-devkit/build-optimizer: 0.0.30
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.34
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0-rc.3
@schematics/angular: 0.0.48
typescript: 2.3.4
webpack: 3.8.1

Repro steps.

create angular/cli project
add dependency and import [email protected]
ng serve --aot
open browser

The log given by the failure.

index.js:10 Uncaught ReferenceError: global is not defined
    at eval (index.js:10)
    at Object.../../../../has-binary2/index.js (vendor.bundle.js:2914)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (index.js:8)
    at Object.../../../../socket.io-parser/index.js (vendor.bundle.js:9569)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (index.js:7)
    at Object.../../../../socket.io-client/lib/index.js (vendor.bundle.js:9527)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (io-service.ts:1)
@dottodot
Copy link

I seeing the same issue as well with ng-socket-io but also with import 'intl'; in polyfills.ts

@amitport
Copy link
Author

amitport commented Oct 24, 2017

  • I tried adding (window as any).global = window; to pollyfils.ts, but it didn't work.
  • reverting to 1.5.0-rc.1 worked

@dottodot
Copy link

Strange I've reverted to 1.5.0-rc.2 and still get it, no idea what's going on.

@amitport
Copy link
Author

amitport commented Oct 24, 2017

@dottodot
oh sorry, I actually reverted to 1.5.0-rc.1
in any case, make sure it's a clean revert. try deleting node_modules and packge/yarn-lock before npm i

@dottodot
Copy link

Thanks that worked. I've gone back to 1.5.0-rc.2 and that's ok for me so seems like the issue is in 1.5.0-rc.3

@Martin-Luft
Copy link

Same problem here since 1.5.0-rc.3, with 1.5.0-rc.2 no problems...

@robisim74
Copy link

Same error using rc.3 Uncaught ReferenceError: global is not defined also importing another library (jsrsasing). It worked fine until rc.2.

@RicardoVaranda
Copy link
Contributor

Hi guys, could you please provide a simple repo of this issue, that way it's easier and faster for the team to be able to debug the issue.

@dottodot
Copy link

@RicardoVaranda https://github.com/dottodot/ang5-test

All i've done is create a new project with 1.5.0-rc.3 and included import 'intl'; in polyfills.ts and you get the error

@filipesilva
Copy link
Contributor

This is definitely related to #8132 as we've removed the node global object from the compilation environment. I'm investigating what can be done about it.

@filipesilva filipesilva self-assigned this Oct 25, 2017
@filipesilva filipesilva added P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful needs: investigation Requires some digging to determine if action is needed type: bug/fix labels Oct 25, 2017
@filipesilva
Copy link
Contributor

Just wanted to let you know we're looking into this. For the intl case using import 'intl/dist/intl'; instead fixes it, but this might not be possible for all libs. socket.io-client also seems to have a dist in socket.io-client/dist/socket.io.js. I don't think the same can be used with ng-socket-io however, since it's importing the node package inside of it...

@MrBlaise
Copy link

Hey, I just wanted to chime in. I have the same issue upgrading to 1.5.0-rc.3 causes this issue, 1.5.0-rc.2 works fine. Didn't change anything just upgraded thew version. I use the following polyfills:

import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/set';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
import 'classlist.js';  // Run `npm install --save classlist.js`.

/** IE10 and IE11 requires the following to support `@angular/animation`. */
import 'web-animations-js';  // Run `npm install --save web-animations-js`.


/** Evergreen browsers require these. **/
import 'core-js/es6/reflect';
import 'core-js/es7/reflect';

/***************************************************************************************************
 * Zone JS is required by Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.

@bogacg
Copy link

bogacg commented Oct 28, 2017

With Rc.6 still having same problem.

@dottodot
Copy link

There's a pull request which I assume will fix it guess we just need to wait for it to be added

#8189

@amitport
Copy link
Author

amitport commented Oct 31, 2017

This is fixed for me with release 1.5.0-rc.8 (#8250). Thanks!

@StojanovicM
Copy link

StojanovicM commented Jan 10, 2018

Hi,
I am using Dragula version 1.5.0 with Angular 5, and still having problem
"ReferenceError: global is not defined"

I have just installed it, imported and added to NgModule -> Imports, and my app crashes with this error.

Is there any step I am missing??

@filipesilva
Copy link
Contributor

filipesilva commented May 2, 2018

On version 6 of Angular CLI we are removing the shim for global and other node built-ins. You can read more about why this change was made in #9827 (comment).

If you are using a library that assumes these globals are present, you can try manually shimming it inside your polyfills.ts file:

// Add global to window, assigning the value of window itself.
(window as any).global = window;

I ran a quick test with socket.io-client and this seemed to allow it to run.

@gioragutt
Copy link
Contributor

@filipesilva Thank you so much!

@krishnareddy226
Copy link

declare var $: any;
I am getting the error
$ not defined

@yulm6
Copy link

yulm6 commented Jul 9, 2018

When I adding sockjs-client, I got the same issue.
npm install sockjs-client --save

I did as @tobias74 ,maybe it worked,but the brower refreshes frequenly.

@chetanbirajdar
Copy link

@tobias74 Thanks that worked

PhilippeCorreges added a commit to jobdoneproject/devoirFaitsFrontNodeJS that referenced this issue Jul 31, 2018
@Evllis
Copy link

Evllis commented Aug 1, 2018

@filipesilva
Thank you!!!!!!!!! ^_^

@SamiHK
Copy link

SamiHK commented Aug 25, 2018

I removed all these things.
import 'intl'; (window as any).global = window; // Run npm install --save intl. import 'intl/locale-data/jsonp/en';

AND NOW :D
This polyfill.ts is working for me.

`import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';

import 'core-js/es7/array';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
import 'classlist.js'; // Run npm install --save classlist.js.

/** IE10 and IE11 requires the following for the Reflect API. */
import 'core-js/es6/reflect';

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es7/reflect';

/**

  • Required to support Web Animations @angular/common/animations.
  • Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation
    **/
    import 'web-animations-js'; // Run npm install --save web-animations-js.

/***************************************************************************************************

  • Zone JS is required by Angular itself.
    */
    import 'zone.js/dist/zone'; // Included with Angular CLI.

/***************************************************************************************************

  • APPLICATION IMPORTS
    */

/**

  • Fix for ngx-chart to work on ie11
    */
    if ( typeof SVGElement.prototype.contains === 'undefined' )
    {
    SVGElement.prototype.contains = HTMLDivElement.prototype.contains;
    }`

@avinexus7
Copy link

On version 6 of Angular CLI we are removing the shim for global and other node built-ins. You can read more about why this change was made in #9827 (comment).

If you are using a library that assumes these globals are present, you can try manually shimming it inside your polyfills.ts file:

// Add global to window, assigning the value of window itself.
(window as any).global = window;

I ran a quick test with socket.io-client and this seemed to allow it to run.

Is this the right way of implementing socket.io in Angular 2? I mean does the above line give any side affects that may be obtrusive in the future?

@BMLande
Copy link

BMLande commented Oct 8, 2018

solution of @filipesilva is worked .
i have issue when i am migrating ng5 to ng6 .
thanks @filipesilva

codyburleson pushed a commit to daleewald/topic-ideation-carbonldp that referenced this issue Dec 1, 2018
lislis added a commit to Datenschule/datenspaziergang-app that referenced this issue Apr 9, 2019
lislis added a commit to Datenschule/datenspaziergang-app that referenced this issue Apr 9, 2019
* update to Abgular 7.2.x

* update travis

* bump more packages

* at least it builds

* update travis

* bump package

* bump mapbox packages

* bump codelyzer

* bump core js

* fix polyfill error

with this solution angular/angular-cli#8160 (comment)
@fkolar
Copy link

fkolar commented Jul 12, 2019

@filipesilva I see this is pretty old issue but even today, newly generated project and I am testing apolo graphql does not add the global.

so I have to add to my polyfills.ts the (window as any).global = window;

Is this normal ?

@clydin
Copy link
Member

clydin commented Jul 12, 2019

Yes. global does not exist in a browser. A library that supports web browsers should not be using Node.js specific functionality.

@willkara
Copy link

Will this ever be fixed in the main module? Still apparently happens in new projects.

I've tried adding a basic AwsAmplify project and this happens.

@amitport
Copy link
Author

amitport commented Jul 31, 2019

@willkara, read the comment right before yours.

Follow the stack trace to see which package uses global. submit an actionable, reproducible issue on that package repo

(also, "happens in new projects." is very different from happened when you "tried adding a basic AwsAmplify")

@willkara
Copy link

@amitport fair comment. I'll follow the trace again and see where it happens.

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

@amitport
Copy link
Author

@filipesilva can you lock this issue? seem to keep "sprouting" in all directions even though it's closed.

@ar1stio
Copy link

ar1stio commented Sep 4, 2019

just add (window as any).global = window; in polyfills.ts in ionic

@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 Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: investigation Requires some digging to determine if action is needed P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful type: bug/fix
Projects
None yet