Filesize Utility

11.0.15 · active · verified Sun Apr 19

The `filesize` library is a lightweight, high-performance JavaScript utility designed to convert byte values into human-readable strings, such as "1.02 kB" or "1 MiB". Currently at version 11.0.15, it emphasizes reliability and efficiency with zero external dependencies and 100% test coverage. Key differentiators include full TypeScript support, robust internationalization via the `Intl` API, `BigInt` compatibility for extremely large numbers, and support for multiple unit standards (SI, IEC, JEDEC). It offers a functional API with partial application for creating reusable formatters and is fully compatible with both Node.js and browser environments. While a specific release cadence isn't published, the project appears actively maintained with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic filesize conversion, using different standards (SI, IEC), partial application for reusable formatters, and various output formats including string and object.

import { filesize, partial } from 'filesize';

// Convert bytes to a human-readable string using default (SI) standard
const sizeInBytes = 265318;
console.log(`Default SI format: ${filesize(sizeInBytes)}`); // "265.32 kB"

// Use IEC standard with base 2 for binary sizes
const binarySize = 1048576; // 1 MiB
console.log(`IEC binary format: ${filesize(binarySize, { standard: 'iec', base: 2 })}`); // "1 MiB"

// Create a partially applied formatter for consistent styling
const formatBinaryIEC = partial({ standard: 'iec', base: 2, round: 0, spacer: '' });
console.log(`Formatted with partial application: ${formatBinaryIEC(binarySize)}`); // "1MiB"

// Output as an object for programmatic access
const sizeObject = filesize(1536, { output: 'object' });
console.log(`Output as object: ${JSON.stringify(sizeObject)}`); // {"value":1.54,"symbol":"kB","exponent":1,"unit":"kB"}

view raw JSON →