node:zlib API Reference

Deflate, Gzip, Brotli, Zstandard, and CRC32

node:zlib supports callback, synchronous, and stream APIs.

FamilySupport
DeflateWrapped and raw compress/decompress functions, Sync variants, classes, and create* helpers
GzipGzip, gunzip, unzip, Sync variants, classes, and create* helpers
BrotliCompress/decompress functions, classes, and create* helpers
ZstandardCompress/decompress functions, classes, and create* helpers
Utilitiescrc32, constants, and codes
const zlib = require("node:zlib");
const input = Buffer.from("hello, zlib!");

const gzip = zlib.gzipSync(input);
zlib.gunzipSync(gzip).toString(); // "hello, zlib!"

const zstd = zlib.zstdCompressSync(input);
zlib.zstdDecompressSync(zstd).toString(); // "hello, zlib!"

Callback forms receive (error, result); synchronous forms return a Buffer or throw.

node:zlib/iter

The Elide-specific iterator module supplies transform factories for gzip, deflate, raw deflate, Brotli, and Zstandard. A factory returns an object with .transform(asyncIterable); output arrives in batches of byte chunks.

import { gzip } from "node:zlib/iter";

async function* input() {
  yield new TextEncoder().encode("hello");
}

for await (const batch of gzip().transform(input())) {
  for (const chunk of batch) {
    // compressed bytes
  }
}

Use Compression Streams with Web Streams.

See also