Convert bytes to a human readable string: 1337 → 1.34 kB
pretty-bytes is a focused utility that converts numeric byte values into human-readable string representations. Created by Sindre Sorhus, it transforms raw numbers like 1337 into '1.34 kB' or 1048576 into '1 MB', solving the common problem of displaying file sizes and data measurements in user interfaces. The package uses SI units (base-10: kB, MB, GB) by default, matching how storage manufacturers and most modern systems present capacity.
With over 21 million weekly downloads, pretty-bytes has become a standard dependency in build tools, CLIs, dashboards, and web applications that handle file operations. Its zero-dependency architecture and minimal footprint make it suitable for both browser bundles and Node.js backends. The package maintains a simple API surface—a single function that takes bytes and optional configuration—avoiding feature bloat while covering the most common formatting scenarios.
The package is part of the broader ecosystem of developer utilities maintained by Sindre Sorhus, known for reliability and consistent API design. At version 7.1.0, it uses ES modules by default while maintaining CommonJS compatibility for older projects. The implementation handles edge cases like negative numbers, very large values, and locale-specific formatting through its options API, though most developers use it with default settings for straightforward byte-to-string conversion.
import prettyBytes from 'pretty-bytes';
// Basic usage with default SI units
console.log(prettyBytes(1337));
// => '1.34 kB'
console.log(prettyBytes(1048576));
// => '1 MB'
// Handle file upload progress
const uploadedBytes = 45_234_567;
const totalBytes = 100_000_000;
console.log(`${prettyBytes(uploadedBytes)} of ${prettyBytes(totalBytes)}`);
// => '45.2 MB of 100 MB'
// Use binary units (base-1024) for OS-accurate sizes
console.log(prettyBytes(1024, { binary: true }));
// => '1 KiB'
// Customize decimal places
console.log(prettyBytes(1337, { minimumFractionDigits: 2, maximumFractionDigits: 3 }));
// => '1.337 kB'
// Locale-specific formatting
console.log(prettyBytes(1234567, { locale: 'de-DE' }));
// => '1,23 MB' (German formatting with comma)
// Handle edge cases
console.log(prettyBytes(0));
// => '0 B'
console.log(prettyBytes(-1337));
// => '-1.34 kB'
// Real-world: format file list for CLI output
const files = [
{ name: 'bundle.js', size: 245678 },
{ name: 'styles.css', size: 34567 },
{ name: 'logo.png', size: 8901 }
];
files.forEach(file => {
console.log(`${file.name.padEnd(20)} ${prettyBytes(file.size).padStart(10)}`);
});
// => bundle.js 240 kB
// styles.css 34.6 kB
// logo.png 8.9 kBFile upload interfaces: Display progress indicators and file size limits in forms, showing users '5 MB of 10 MB uploaded' instead of raw byte counts that require mental math.
Cloud storage dashboards: Present account usage statistics in admin panels or user settings, converting quota consumption from database integers (bytes) into readable formats like '43.2 GB of 100 GB used'.
Build tool output: CLI tools like bundlers and package managers use it to report asset sizes, dependency footprints, or cache storage in terminal output—webpack plugins, npm scripts, and CI/CD pipelines commonly integrate it.
System monitoring tools: Node.js applications tracking memory usage, disk space, or network transfer rates format runtime metrics for logs, dashboards, or alerting systems.
API responses: Backend services include formatted file size strings alongside raw byte counts in JSON responses, reducing client-side formatting logic and ensuring consistent display across multiple frontend platforms.
npm install pretty-bytespnpm add pretty-bytesbun add pretty-bytes