An ESM-only build of lz-string, the LZ-based compression library for JavaScript.
This package is not a fork or a rewrite. The original pieroxy/lz-string repository is included as a git submodule in vendor/lz-string and stays the single source of truth for the algorithm. src/index.ts only re-exports its APIs, and tsdown recompiles and bundles them from that TypeScript source into a modern distribution:
- ESM only
- Named exports only (no default export), tree-shakable,
sideEffects: false - Bundled type declarations
- No Node.js dependency: the
loadBinaryFile/saveBinaryFilehelpers and the CLI are not included
Any fix or change to the compression logic belongs upstream.
npm i lz-string-esimport { compressToBase64, decompressFromBase64 } from 'lz-string-es'
const compressed = compressToBase64('Hello, Hello, Hello, Hello!')
const text = decompressFromBase64(compressed)The API is the same as lz-string: compress, compressToBase64, compressToUTF16, compressToUint8Array, compressToEncodedURIComponent, compressToCustom, and their decompress* counterparts. See the upstream documentation for details.
As lz-string-es no longer ships a default export, you need to import it as an object to access all its functions.
import * as lz from 'lz-string-es'
const compressed = lz.compressToBase64('Hello, Hello, Hello, Hello!')
const text = lz.decompressFromBase64(compressed)The loadBinaryFile and saveBinaryFile helpers are not included in this package, as it has no Node.js dependency. You can implement them with:
import { readFileSync, writeFileSync } from 'node:fs'
import { convertFromUint8Array, convertToUint8Array } from 'lz-string-es'
export function saveBinaryFile(fileName: PathOrFileDescriptor, data: string | Uint8Array) {
writeFileSync(fileName, typeof data === 'string' ? convertToUint8Array(data)! : data, null)
}
export function loadBinaryFile(fileName: PathOrFileDescriptor) {
return convertFromUint8Array(readFileSync(fileName, null))
}git clone --recurse-submodules https://github.com/antfu/lz-string-es.git
pnpm install # also runs `git submodule update --init` via the prepare script
pnpm buildTo update to the latest upstream:
git -C vendor/lz-string pull origin master
git add vendor/lz-stringAll credit for the compression algorithm and its implementation goes to Pieroxy and the lz-string contributors. The algorithm is based on LZ compression; this package only changes how it is distributed.
MIT License
- © 2013 Pieroxy, original lz-string
- © 2025-PRESENT Anthony Fu, ESM distribution