{"id":11518,"library":"pako","title":"Pako - JavaScript Zlib Compression","description":"Pako is a high-performance JavaScript port of the zlib library, designed for fast data compression and decompression directly within web browsers and Node.js environments. The current stable version is 2.1.0. It aims to provide binary-compatible results with the original C zlib implementation (v1.2.8 as per the README), demonstrating near-native speed in modern JavaScript engines for CPU-intensive tasks. Pako differentiates itself by its modular design, allowing individual components to be used or browserified, and its support for both raw deflate/inflate streams and gzip format. It handles various input types, including automatic UTF-8 recoding for strings during compression and optional UTF-8 string output for decompression. Its release cadence is generally feature-driven and as needed, rather than fixed time intervals.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/nodeca/pako","tags":["javascript","zlib","deflate","inflate","gzip"],"install":[{"cmd":"npm install pako","lang":"bash","label":"npm"},{"cmd":"yarn add pako","lang":"bash","label":"yarn"},{"cmd":"pnpm add pako","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For pako v2.x, the main export is a CommonJS module. Direct ES module imports may behave unexpectedly or require bundler configuration for CJS interop. Pako v3.x transitioned to ESM-first.","wrong":"import pako from 'pako';","symbol":"pako","correct":"const pako = require('pako');"},{"note":"In pako v2.x, `Deflate` is a named property of the main CommonJS export. For pako v3.x, `Deflate` is a top-level named ES module export.","wrong":"import { Deflate } from 'pako';","symbol":"Deflate class","correct":"const { Deflate } = require('pako');"},{"note":"In pako v2.x, `inflate` is a named property of the main CommonJS export. For pako v3.x, `inflate` is a top-level named ES module export.","wrong":"import { inflate } from 'pako';","symbol":"inflate function","correct":"const { inflate } = require('pako');"}],"quickstart":{"code":"const pako = require('pako');\n\nconst data = { message: 'Hello, pako!', timestamp: Date.now(), items: ['a', 'b', 'c'] };\nconst stringifiedData = JSON.stringify(data);\n\nconsole.log('Original size (bytes):', Buffer.byteLength(stringifiedData, 'utf8'));\n\n// Compress stringified data\nconst compressed = pako.deflate(stringifiedData, { level: 9 }); // Use highest compression level\nconsole.log('Compressed size (bytes):', compressed.length);\n\n// Decompress back to string\nconst restoredString = pako.inflate(compressed, { to: 'string' });\nconsole.log('Decompressed string matches original:', restoredString === stringifiedData);\n\nconst restoredData = JSON.parse(restoredString);\nconsole.log('Restored data:', restoredData);\n\n// Demonstrating stream interface for larger data\nconst deflator = new pako.Deflate();\ndeflator.push(new TextEncoder().encode('First chunk of data. '), false);\ndeflator.push(new TextEncoder().encode('Second chunk, and this is the end.'), true);\n\nif (deflator.err) {\n  console.error('Deflate error:', deflator.msg);\n} else {\n  console.log('Stream compressed data length:', deflator.result.length);\n}","lang":"javascript","description":"Demonstrates basic string compression/decompression using `deflate` and `inflate` with string conversion, including an example of the streaming API for larger data handling."},"warnings":[{"fix":"For CommonJS environments, explicitly import from `pako/index.cjs` or `pako/dist/pako.cjs.js`. For ESM, use named imports like `import { deflate, inflate } from 'pako';` and adjust code to new top-level exports.","message":"Starting with v3.0.0, Pako transitioned to an ESM-first package. While bundlers may handle CJS interop, direct `require('pako')` for the main entry point will lead to issues, and `import pako from 'pako'` will behave differently or require specific build configurations compared to v2.x.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Update imports from `const pako = require('pako'); pako.deflate(...)` to `import { deflate } from 'pako'; deflate(...)` (for ESM) or `const { deflate } = require('pako/index.cjs'); deflate(...)` (for CJS).","message":"In Pako v3.x, methods like `deflate`, `inflate`, `deflateRaw`, and `inflateRaw` are now direct named exports from the top-level `pako` module, rather than properties of a default `pako` object (e.g., `pako.deflate`). This changes the way these functions are accessed.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always check `if (deflator.err)` or `if (inflator.err)` after pushing chunks or at the end of the stream processing to ensure data integrity and proper error handling.","message":"When using the streaming `Deflate` or `Inflate` classes, errors are not thrown as exceptions directly but are instead indicated by the `err` property on the instance after `push` operations. Failing to check `deflator.err` or `inflator.err` can lead to silent data corruption or incomplete results.","severity":"gotcha","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For Pako v2.x, ensure your environment supports CommonJS or use a bundler (like Webpack or Rollup) configured for CJS interop. For Pako v3.x, switch to ES module imports: `import { deflate } from 'pako';`","cause":"Attempting to use `require()` in an ES module environment (e.g., Node.js with `\"type\": \"module\"` in `package.json`, or a browser without a bundler that transpiles CJS).","error":"ReferenceError: require is not defined"},{"fix":"For Pako v3, change your import to `import { deflate, inflate } from 'pako';` and call them directly as `deflate(...)`, `inflate(...)`. If using v2 with a bundler that mishandles default exports, try `import * as pako from 'pako';` or stick to `const pako = require('pako');`.","cause":"This typically occurs when using `import pako from 'pako'` with Pako v3, or when mixing CJS/ESM import styles. In v3, `deflate` (and other core functions) are named exports, not properties of a default `pako` object.","error":"TypeError: pako.deflate is not a function"},{"fix":"Verify the integrity and completeness of your compressed data. Ensure that the `format` option for `inflate` (e.g., `windowBits`) matches the format used during compression. For example, use `pako.inflate(data, { raw: true })` for raw deflate data, or `pako.inflate(data, { to: 'string' })` if expecting UTF-8 output from a compatible `deflate` operation.","cause":"The input data provided to `pako.inflate` (or `Inflate` class) is corrupted, incomplete, or was compressed with a different format than expected (e.g., raw deflate data passed to `inflate` expecting gzip, or vice versa).","error":"Error: incorrect header check"}],"ecosystem":"npm"}