{"id":18237,"library":"compression-with-brotli","title":"compression","description":"Node.js compression middleware for Express/Connect that supports gzip, deflate, and brotli response compression. Current stable version is 1.8.1, released March 2025, with a moderate release cadence. Key differentiators: built-in brotli support (requires Node >= v11.7.0 or v10.16.0), automatic filtering based on Content-Type via the compressible package, and flexible configuration of compression levels and memory usage. It is part of the Express.js ecosystem and is widely used with Express and plain Node HTTP servers.","status":"active","version":"1.7.5","language":"javascript","source_language":"en","source_url":"https://github.com/expressjs/compression","tags":["javascript"],"install":[{"cmd":"npm install compression-with-brotli","lang":"bash","label":"npm"},{"cmd":"yarn add compression-with-brotli","lang":"bash","label":"yarn"},{"cmd":"pnpm add compression-with-brotli","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Negotiates content encoding","package":"accepts","optional":false},{"reason":"Parses byte size strings","package":"bytes","optional":false},{"reason":"Determines if content type is compressible","package":"compressible","optional":false},{"reason":"Debug logging","package":"debug","optional":false},{"reason":"Monitors response headers","package":"on-headers","optional":false},{"reason":"Buffer API polyfill","package":"safe-buffer","optional":false},{"reason":"Adds Vary header","package":"vary","optional":false}],"imports":[{"note":"CommonJS only; no ESM wrapper. Use require().","wrong":"import compression from 'compression'","symbol":"default","correct":"const compression = require('compression')"},{"note":"Default export, not named. For ESM environments, use the default import.","wrong":"import { compression } from 'compression'","symbol":"default","correct":"import compression from 'compression'"},{"note":"The package exports a single function directly, not an object.","wrong":"const { compression } = require('compression')","symbol":"compression","correct":"const compression = require('compression')"}],"quickstart":{"code":"const express = require('express');\nconst compression = require('compression');\n\nconst app = express();\n\n// Enable compression for all responses\napp.use(compression());\n\n// Route that returns a large response\napp.get('/large', (req, res) => {\n  const largeString = 'x'.repeat(1024 * 100); // 100KB string\n  res.send(largeString);\n});\n\napp.listen(3000, () => {\n  console.log('Server running on port 3000');\n});","lang":"javascript","description":"Basic Express server with compression middleware. Handles a 100KB string response that will be compressed."},"warnings":[{"fix":"Upgrade Node.js to v11.7.0+ or v10.16.0+ for brotli support.","message":"Brotli compression requires Node.js >= v11.7.0 or v10.16.0. Older Node versions will fall back to gzip.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set level explicitly: compression({ level: 6 }).","message":"The option 'level' with value -1 (default) is equivalent to zlib.Z_DEFAULT_COMPRESSION (level 6). Use explicit level for clarity.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Remove no-transform directive if compression is desired.","message":"Compression is skipped for responses with Cache-Control: no-transform. Be aware if you're using CDNs or reverse proxies.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure client sends Accept-Encoding header.","message":"The middleware compresses only if the request's Accept-Encoding header includes the encoding. Client must support it.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the client sends Accept-Encoding header, e.g., 'gzip, deflate'.","cause":"Request without Accept-Encoding header","error":"Error: Compression requires a valid Accept-Encoding header"},{"fix":"Make sure the response body is a stream or string/buffer.","cause":"Response body is not a stream or is null","error":"TypeError: Cannot read property 'pipe' of undefined"},{"fix":"Ensure no early res.end() calls; use res.write() properly.","cause":"Response already sent before compression completes","error":"Error: write after end"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}