Registry / web-framework / compresion

compresion

JSON →
library1.7.11jsnpmunverified

Node.js compression middleware for Express/Connect that provides gzip and deflate encoding. Current stable version is 1.8.1 (August 2024). Maintained by the Express.js team with monthly releases. Handles content negotiation, cache-control respecting, and configurable zlib options. Key differentiator is seamless integration with Express routing, supporting threshold, filter, and level settings. Not a standalone compressor; must be used with Node.js HTTP server or framework. Legacy support for Node >=0.8.0.

npm install compresion
INSTALL
IMPORT
SIG · COMPRESION
C
compresion
web-frameworkjavascriptv1.7.11
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

compression
import compression from 'compression'
const compression = require('compression')
ESM is supported in v1.8+ with conditional exports; default import works. For CommonJS, require('compression') is still valid.
CompressionOptions
import type { CompressionOptions } from 'compression'
TypeScript users can import the options interface for type safety.

Creates an Express server with compression middleware applied globally to compress responses.

import compression from 'compression'; import express from 'express'; const app = express(); // apply compression middleware app.use(compression()); // normal route app.get('/data', (req, res) => { res.json({ message: 'This response will be compressed' }); }); app.listen(3000, () => console.log('Server listening on port 3000'));
Debug
Known issues
breakingIn v1.7.0, changed from using raw Buffer to safe-buffer for improved Buffer API compatibility. This may affect custom compression filter functions that rely on Buffer constructors.
fix
Update any code that explicitly uses Buffer to use safe-buffer's Buffer if needed.
affects: >=1.7.0
deprecatedIn v1.8.0, the `_header` property is deprecated in favor of `headersSent`. Code that directly checks `res._header` may break in future Node.js versions.
fix
Replace `res._header` with `res.headersSent` in custom filter or hook functions.
affects: >=1.8.0
gotchaDo NOT call `res.end()` or pipe the response before compression middleware runs. The middleware must intercept the response stream; otherwise compression will not be applied and may cause errors.
fix
Ensure `compression()` is inserted in middleware stack before any route handlers that write to the response.
affects: *
gotchaCompression is automatically skipped if the response has `Cache-Control: no-transform`. This is a feature to respect CDN and proxy transformations, but can be surprising if you expect compression overrides.
fix
Remove `no-transform` from Cache-Control header if you want forced compression.
affects: *
breakingIn v1.6.0, the default `level` changed from 6 to -1 (zlib default). With newer Node versions, the underlying zlib default may vary, affecting compression ratio consistency.
fix
Explicitly set `level` option if you need a specific compression level.
affects: >=1.6.0
Errors
Common errors & fixes
TypeError: compression is not a function
Incorrect import: using `require('compression')` in an ESM environment or importing as default without default import.
fix
Use `import compression from 'compression'` or use `const compression = require('compression').default` if using CommonJS in ESM context.
ERR_INVALID_ARG_TYPE: The "level" argument must be of type number. Received undefined
Passing options to `compression()` where `level` is not a number (e.g., string).
fix
Ensure `level` option is a number between -1 and 9.
Cannot set property 'writeHead' of undefined
Compression middleware was applied after response has already been written or ended (e.g., placed after route handlers).
fix
Move `app.use(compression())` before all route handlers.
Upgrade
Version history
1.7.11latest on npm
Audit
Dependencies
acceptsrequiredcontent negotiation for compression encoding
bytesrequiredparse content-length and threshold values
compressiblerequiredcheck if response content type is compressible
debugrequireddebugging and logging
on-headersrequiredhook into response headers to apply compression before write
safe-bufferrequiredpolyfill for Buffer API (used in older Node versions)
varyrequiredset Vary header for content negotiation
Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources