Using Zlib

Hi guys

I’m strugling my self trying to make it work with Zlib library and unzip method.

Basically I’ve created an Ionic v4 blank application and import Zlib library:

import { Component } from '@angular/core';
import * as zlib from 'zlib';
import { Buffer } from 'buffer';

  selector: 'app-home',
  templateUrl: '',
  styleUrls: [''],
export class HomePage {

  constructor() {

  test_zlib() {   
    const input = '.................................';
    zlib.deflate(input, function(err, buffer) {
      if (!err) {

Running the example using nodejs console is ok, but running ionic serve, navigator triggers an error showing:

core.js:9110 ERROR Error: Uncaught (in promise): ReferenceError: Buffer is not defined
ReferenceError: Buffer is not defined
at Object…/node_modules/core-util-is/lib/util.js (util.js:103)
at __we

Are you so kind to check it and let me know what I’m doing wrong?

Welcome! As it’s your first post here, perhaps you’re not familiar with my general disdain for the entire world of JavaScript, but here’s one more thing to hate: there are lots of libraries that expect to be running under Node.js, and don’t deal real well with life in a browser. You appear to have stumbled upon one of these. I would look for a replacement that advertises browser support: perhaps this one?

I’m already working with Pako library, thank you for the tip @rapropos.

Somewhere I did see that javascript was the “hatest and lovest” programming language, I’m starting to understand why.

In reference to zlib it self, why are you so sure that the LIB doens’t work with browser? I’ll like to do not invest a whole day next time I can’t make work a nodejs library in the browser.

Ironically, pretty much the only thing I like about it - the fact that it’s easy to implement interpreters for it - is largely the reason I have to deal with the giant mountain of things I hate about it, because it’s so universal.

I guess it’s not really zlib itself, but that implementation you’re working with. My mental checklist for “might this be a node/browser concern?” goes something like this:

  • are we dealing with a “headless” library, that is primarily concerned with munging data about, instead of doing something visual? if so, raise probability
  • are we seeing uncaught exceptions that spawn completely within the library, with no app code involved whatsoever (yes, in your case)? if so, raise probability
  • do they involve undefined identifiers, especially some of the usual suspects in these cases like buffer, fs or crypto? if so, raise probability
  • look at the project’s documentation and bug list - is it supposed to work in the browser? if so, lower probability.
  • are people using it a browser environment? if so, lower probability
  • are people trying to use it in a browser environment, failing, and filing bugs? if so, raise probability greatly
  • did OP try running this library under node and had no problems? (OK, this has never happened before, but you did do it, which was a HUGE help to me) if so, raise probability through the roof

Very clear and helpful to me