prettysize

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

A lightweight utility to convert byte counts into human-readable strings (e.g., 1024 → "1 kB") for logging and CLI output. Version 2.0.0 supports sizes from bytes up to exabytes, with options to remove spaces, use single-character suffixes, control decimal places, or return only the numeric value. Minimal dependencies, pure JavaScript, works in Node.js and browsers. Alternatives like filesize.js offer more formatting features, but prettysize focuses on simplicity and small footprint.

error TypeError: pretty is not a function
cause CommonJS require returns a module object; destructuring incorrectly.
fix
Use const pretty = require('prettysize'); not const { pretty } = require('prettysize');
error Error [ERR_REQUIRE_ESM]: require() of ES Module
cause Trying to require the package in a strict ESM context without proper handling.
fix
Use import pretty from 'prettysize' (with bundler) or createRequire from 'module'.
breaking Version 2.0.0 changed the default decimal places from 0 to 1.
fix Update code to expect one decimal place by default, or explicitly set places option.
deprecated Positional arguments (true, true, 2) are deprecated in favor of options object.
fix Use options object: { nospace: true, one: true, places: 2 } instead of positional args.
gotcha The function returns a string with a space by default (e.g., '1 kB'), which may not match expectations for pure numeric output.
fix Use { nospace: true } to remove space, or { numOnly: true } to get a number directly.
npm install prettysize
yarn add prettysize
pnpm add prettysize

Demonstrates basic usage with a byte value, shows options for no space, single character suffix, decimal places, and numeric-only output.

const pretty = require('prettysize');

// Basic usage
console.log(pretty(1024));           // "1 kB"
console.log(pretty(1048576));        // "1 MB"

// Options: remove space, single character, decimal places
console.log(pretty(123456789, { nospace: true }));         // "117.7M"
console.log(pretty(123456789, { one: true, places: 2 }));  // "117.74 M"
console.log(pretty(123456789, { numOnly: true }));         // 117.7