lws-compress

raw JSON →
3.1.0 verified Sat Apr 25 auth: no javascript

Response compression middleware for the lws (local-web-server) framework, enabling gzip compression for HTTP responses. Current stable version is 3.1.0, compatible with Node.js >=12.17. It adds --compress and --compress.threshold CLI options to lws, with a default compression threshold of 1024 bytes. Published under the lwsjs organization, this middleware is a lightweight, focused solution for local development servers, following the standard.js code style.

error SyntaxError: Named export 'compress' not found. The requested module 'lws-compress' is a CommonJS module, which may not support all module.exports as named exports.
cause Using named import { compress } from 'lws-compress' with ESM import syntax, but the package no longer provides that named export (since v3).
fix
Use default import: import compress from 'lws-compress'
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/lws-compress/index.js from /path/to/project/app.js not supported.
cause Attempting to use require() with an ESM-only package (v3+).
fix
Switch to ESM: use import or type: 'module' in package.json. Or use dynamic import: const compress = (await import('lws-compress')).default
error TypeError: compress is not a function
cause Importing the module incorrectly, e.g., using named import when default import is required.
fix
Ensure you use default import: import compress from 'lws-compress' or const compress = require('lws-compress').default
breaking v3 changed from CommonJS to ESM-only. This breaks require() calls in Node.js versions <12.17 or projects not using ESM.
fix Use ESM imports (import compress from 'lws-compress') or upgrade to Node.js >=12.17 and set type: 'module' in package.json. For CJS, use dynamic import: const compress = (await import('lws-compress')).default.
breaking v3 removed the named export 'compress'. Previously both default and named exports were available.
fix Use default import only: import compress from 'lws-compress'.
gotcha Compression threshold is in bytes, not kilobytes. Setting threshold to 1 applies compression to responses >=1 byte, effectively always compressing.
fix Ensure threshold is specified in bytes (e.g., 1024 for 1KB).
gotcha The --compress option must be explicitly passed to enable compression. Without it, no compression occurs even if the middleware is in the stack.
fix Use --compress (or -z) when launching lws, or pass compress: true in lws options.
gotcha Compression only applies to responses with content-type that are compressible (e.g., text/html, application/json). Binary files may be skipped.
fix Check lws documentation for compressible content types or implement custom logic.
npm install lws-compress
yarn add lws-compress
pnpm add lws-compress

Starts an lws server with response compression middleware, setting a custom compression threshold of 2048 bytes.

import lws from 'lws'
import compress from 'lws-compress'

const server = lws({
  port: 8080,
  stack: [compress],
  compress: {
    threshold: 2048
  }
})
console.log('Server running on http://localhost:8080')