Gzipper CLI and Module
Gzipper is a versatile command-line interface (CLI) tool and Node.js module designed for efficient file compression. It supports a comprehensive range of modern compression algorithms, including Deflate, Brotli, gzip, Zopfli, and zstd, providing developers with extensive control over the compression process. The current stable version is 8.2.1, with a regular release cadence as features and fixes are developed. Its key differentiators include fine-grained control over algorithm-specific parameters (e.g., `--gzip-level`, `--brotli-quality`), support for incremental compression, verbose output, and seamless integration with popular CLI UI tools like Angular CLI. It can compress entire directories by default or be configured with include/exclude patterns, and allows for custom output file formats. Environment variables take precedence over CLI arguments for configuration.
Common errors
-
gzipper: command not found
cause `gzipper` is not installed globally or is not in your system's PATH.fixInstall `gzipper` globally using `npm i -g gzipper` or ensure your shell's PATH includes `$(npm bin -g)`. -
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/gzipper/dist/index.js from .../my-script.js not supported.
cause You are trying to `require()` gzipper in a CommonJS module, but gzipper's main entry point might be an ESM module or your project configuration is conflicting.fixEither convert your consuming script to an ES Module (e.g., add `"type": "module"` to package.json or use `.mjs` extension) and use `import { compress } from 'gzipper';`, or ensure you are using the CommonJS compatible entry point with `const { compress } = require('gzipper/cjs');` if available, or simply `require('gzipper');` which should resolve correctly based on the package.json `exports` field. -
Error: The 'path' argument must be of type string or an instance of Buffer or URL. Received undefined
cause The input or output directory arguments to the `compress` function (or CLI) were not provided or were undefined.fixEnsure that the input path and output path arguments are correctly provided as strings to the `compress` function or CLI command.
Warnings
- breaking Gzipper requires Node.js version 20.11.0 or higher. Running it with older Node.js versions will result in execution errors.
- gotcha By default, `gzipper` will compress ALL files in the specified input directory. This can lead to unintended compression of files you don't want processed if `--include` or `--exclude` options are not used.
- gotcha When using `gzipper` programmatically or via CLI, environment variables with option names (e.g., `GZIPPER_BROTLI_QUALITY=11`) will override any corresponding CLI arguments or programmatic options passed directly to the `compress` function.
- gotcha When using multiple compression algorithms (e.g., both `--gzip` and `--brotli`), ensure your `--output-file-format` or algorithm-specific output format options (e.g., `--gzip-output-file-name-format`) prevent filename collisions, otherwise files might be overwritten.
Install
-
npm install gzipper -
yarn add gzipper -
pnpm add gzipper
Imports
- compress
const { compress } = require('gzipper');import { compress } from 'gzipper'; - compress
const { compress } = require('gzipper'); - GzipperOptions
import type { GzipperOptions } from 'gzipper';
Quickstart
import { compress } from 'gzipper';
import * as fs from 'node:fs';
import * as path from 'node:path';
const tempDir = path.join(process.cwd(), 'temp-gzipper-test');
const dummyFile = path.join(tempDir, 'example.txt');
async function runCompression() {
// Create a temporary directory and file for testing
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
fs.writeFileSync(dummyFile, 'This is a test file to demonstrate gzipper compression. It should be long enough to get some compression benefit.', 'utf8');
console.log('Compressing files in', tempDir);
try {
await compress(tempDir, tempDir, {
brotli: true, // Enable Brotli compression
gzip: true, // Also enable Gzip compression
outputFileFormat: '[name].[ext].br', // Brotli output format
gzipOutputFileNameFormat: '[name].[ext].gz', // Gzip specific output format
verbose: true,
removeLarger: true // Remove original if compressed is smaller
});
console.log('Compression complete. Checking created files...');
console.log('Files in temp directory:', fs.readdirSync(tempDir));
} catch (error) {
console.error('Compression failed:', error);
} finally {
// Clean up temporary files and directory
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
console.log('Cleaned up temporary directory:', tempDir);
}
}
}
runCompression();