Angular 7: Uncaught ReferenceError: global is not defined when adding package

See original GitHub issue

I am building an Angular 7 app, and when i add a package npm install dragula and import this into thepollyfills.tsfile i get this error:

index.js:2 Uncaught ReferenceError: global is not defined at Object…/node_modules/custom-event/index.js (index.js:2) at webpack_require (bootstrap:83) at Object…/node_modules/crossvent/src/crossvent.js (crossvent.js:3) at webpack_require (bootstrap:83) at Object…/node_modules/dragula/dragula.js (dragula.js:4) at webpack_require (bootstrap:83) at Module…/src/polyfills.ts (polyfills.ts:1) at webpack_require (bootstrap:83) at Object.1 (polyfills.ts:92) at webpack_require (bootstrap:83)

When I googled it everyone says to add this (window as any).global = window; to pollyfills.ts I have done this and I still get the error. I also ready to delete the node_modules folder and npm i I have done this as well.

I do not know what else to do here. can anyone please tell me some sugestions or at least explain why this would happen?

This may not be relevant but I’m going to add it as well. There is another error which wasnt there before I imported them into pollyfills.ts

accordion-group.component.ts:9 Uncaught ReferenceError: global is not defined at Module…/src/app/components/accordion/accordion-group.component.ts (accordion-group.component.ts:9) at webpack_require (bootstrap:83) at Module…/src/app/components/accordion/accordion.module.ts (accordion.component.ts:10) at webpack_require (bootstrap:83) at Module…/src/app/shared/shared.module.ts (window.extensions.ts:15) at webpack_require (bootstrap:83) at Module…/src/app/app.module.ts (app.constants.ts:17) at webpack_require (bootstrap:83) at Module…/src/main.ts (main.ts:1) at webpack_require (bootstrap:83)

  • Read the contributing guidelines
  • Bug reports containing repro steps are likely to be fixed faster
  • Feature requests should be multi-purpose, describe use cases
  • For support requests or questions, please refer to our Slack channel

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

48reactions
hexcowboycommented, Sep 3, 2021

Fixed by adding this to vite.config.ts

export default defineConfig({
  ...
  define: {
    "global": {},
  },
});
27reactions
xingorg1commented, Apr 26, 2022

I encountered the same problem 😭😭😭, and finally solved it with the following code :

<!-- your index.html -->
<script>
/**
 * this is a hack for dragula
 * error: global is not defined
 */
var global = global || window
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 7: Uncaught ReferenceError: global is not defined ...
When I googled it everyone says to add this (window as any).global = window; to pollyfills.ts I have done this and I still...
Read more >
Node.js – Angular 7: Uncaught ReferenceError: global is not ...
Node.js – Angular 7: Uncaught ReferenceError: global is not defined when adding package ... When I googled it everyone says to add this...
Read more >
How to fix the ReferenceError: global is not defined error in ...
That sound simple enough to fix, just create a vite.config.[js/ts] file, add it to your SvelteKit project and define global right? // vite....
Read more >
uncaught referenceerror: global is not defined - You.com
Angular 7 : Uncaught ReferenceError: global is not defined when adding package ... am building an Angular 7 app, and when i add...
Read more >
Uncaught ReferenceError: global is not defined - YouTube
Uncaught ReferenceError : global is not defined at node_modules/@walletconnect/socket-transport/dist/esm/index.jscreate a n ew filw named ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found