ashtuchkin / iconv-lite

Convert character encodings in pure javascript.
MIT License
3.08k stars 282 forks source link

Error: 'global is not defined' #263

Open RaschidJFR opened 3 years ago

RaschidJFR commented 3 years ago

When importing iconv-lite (v0.6.2) in an Angular 10 app I'm getting this error:

Uncaught ReferenceError: global is not defined
    at Object../node_modules/buffer/index.js (my-component.js:238)
    at __webpack_require__ (runtime.js:85)
    at Object../node_modules/safer-buffer/safer.js (my-component.js:8266)
    at __webpack_require__ (runtime.js:85)
    at Object../node_modules/iconv-lite/lib/index.js (my-component.js:7768)
    at __webpack_require__ (runtime.js:85)
    at Module../src/my-component.ts (my-component.js:11618)
    at __webpack_require__ (runtime.js:85)
    at Object.1 (my-component.js:11705)
    at __webpack_require__ (runtime.js:85)

It looks like the error comes from the dependency safer-buffer, but whilst it gets fixed, a workaround for this is adding this line in index.js:

// node_modules/iconv-lite/lib/index.js
window.global = window;  // Add this just before importaing safer-buffer
var Buffer = require("safer-buffer").Buffer;
RaschidJFR commented 3 years ago

I've created a hotfix but can't create a PR because it is not up to date with master and master is now too far from this version. So If anyone runs across this issue, you can use this fix for now:

npm install raschidjfr/iconv-lite#hotfix/v0.6.3
TheThing commented 3 years ago

Because of the removal of safer-buffer with #265 this might be auto fixed in the next version.