gpmf-extract icon indicating copy to clipboard operation
gpmf-extract copied to clipboard

"Uncaught ReferenceError: global is not defined at..." when using with webpack (Angular 10^)

Open iolyd opened this issue 3 years ago • 1 comments

First of all, thanks for developing this wonderful tool!

On the issue: when using the package in a browser project with webpack, in our case Angular, and importing it with import * as GpmfExtract from 'gpmf-extract';, we receive this error: Uncaught ReferenceError: global is not defined at... which seems to stem from the inline-worker: var WORKER_ENABLED = !!(global === global.window && global.URL && global.Blob && global.Worker);

For the moment, the solution is to declare (window as any).global = window; in the project's polyfills.ts file, but this feels more like a temporary fix than a proper usage.

iolyd avatar Mar 23 '21 13:03 iolyd

Thanks for the report. I personally don't have experience with Angular or TypeScript, so I would appreciate it if someone else suggested a fix for this. Otherwise I'll have a look whenever I find a spare moment.

The module works well in Webpack if using the require syntax.

var gpmfExtract = require('gpmf-extract');

JuanIrache avatar Mar 23 '21 18:03 JuanIrache

The problem is that global is not defined outside of NodeJS. To solve this a globalThis variable was made that is defined both in nodeJS and in the browser. Switching to that should fix the problem. To be more compatible, you could define global as this:

const myGlobal = typeof globalThis ? globalThis : typeof global ? global : window;

Akxe avatar Apr 16 '23 13:04 Akxe

Sounds reasonable. PR welcome

JuanIrache avatar Apr 17 '23 01:04 JuanIrache

This should be fixed now :)

Akxe avatar Apr 20 '23 21:04 Akxe