These libraries handle the conversion of raw byte counts into human-readable strings, though they serve different roles in the stack. bytes is widely used in backend environments, particularly with Express.js, for parsing HTTP headers like Content-Length as well as formatting. filesize offers deep customization for formatting, supporting bits, bytes, and various rounding strategies. pretty-bytes is the frontend standard for clean, localized output with minimal configuration. humanize-bytes provides a lightweight alternative but sees less active maintenance compared to the others.
When displaying storage capacity, network speed, or file sizes, raw numbers like 1048576 mean nothing to users. These four packages solve that problem, but they target different layers of the application stack. Let's break down how they handle parsing, formatting, and localization.
The most critical difference lies in direction. Most tools only format numbers to strings, but bytes handles both directions, making it unique for server-side input handling.
bytes parses strings into numbers and formats numbers into strings.
Content-Length headers or config files.null if the string is invalid.import bytes from 'bytes';
// Format number to string
console.log(bytes(1024)); // '1KB'
// Parse string to number
console.log(bytes('1kb')); // 1024
filesize focuses strictly on formatting numbers into readable strings.
import { filesize } from 'filesize';
// Format number to string
console.log(filesize(1024)); // '1.02 kB'
// Parse string to number (Not supported)
// console.log(filesize('1kb')); // Error
humanize-bytes is a simple formatter for numbers.
import humanize from 'humanize-bytes';
// Format number to string
console.log(humanize(1024)); // '1KB'
// Parse string to number (Not supported)
// console.log(humanize('1kb')); // Error
pretty-bytes is a dedicated formatter optimized for UI display.
import prettyBytes from 'pretty-bytes';
// Format number to string
console.log(prettyBytes(1024)); // '1.02 kB'
// Parse string to number (Not supported)
// console.log(prettyBytes('1kb')); // Error
Developers often need to tweak rounding, units, or separators. filesize leads here, while pretty-bytes prefers sensible defaults.
bytes allows basic separators and decimal control.
import bytes from 'bytes';
bytes(1000, {
decimalPlaces: 2,
thousandsSeparator: ','
}); // '1000.00B'
filesize offers the deepest configuration options.
import { filesize } from 'filesize';
filesize(1000, {
round: 3,
base: 2,
bits: true
}); // '7.813 Kib'
humanize-bytes has minimal to no configuration.
import humanize from 'humanize-bytes';
// No options typically supported
humanize(1000); // '1KB'
pretty-bytes balances simplicity with key controls.
import prettyBytes from 'pretty-bytes';
prettyBytes(1000, {
binary: true,
maximumFractionDigits: 1
}); // '1 KiB'
Confusion often arises between KB (1000 bytes) and KiB (1024 bytes). Different packages default to different standards.
bytes defaults to JEDEC (base 1024, no 'i').
1KB even if it means 1024.import bytes from 'bytes';
bytes(1024); // '1KB' (JEDEC style)
filesize lets you choose explicitly.
import { filesize } from 'filesize';
filesize(1024, { standard: 'iec' }); // '1 KiB'
filesize(1024, { standard: 'jedec' }); // '1 KB'
humanize-bytes typically follows JEDEC.
import humanize from 'humanize-bytes';
humanize(1024); // '1KB' (JEDEC style)
pretty-bytes defaults to SI (base 1000) but supports binary.
import prettyBytes from 'pretty-bytes';
prettyBytes(1000); // '1 kB' (SI style)
prettyBytes(1024, { binary: true }); // '1 KiB'
Global applications need number formatting that respects local conventions (e.g., commas vs periods).
bytes has no built-in locale support.
import bytes from 'bytes';
// No locale option
bytes(1000.5); // '1000.5B'
filesize supports locale via options.
import { filesize } from 'filesize';
filesize(1000.5, { locale: 'de-DE' }); // '1.000,5 B'
humanize-bytes lacks locale features.
import humanize from 'humanize-bytes';
// No locale option
humanize(1000.5); // '1KB'
pretty-bytes has robust locale support.
Intl.NumberFormat under the hood.import prettyBytes from 'pretty-bytes';
prettyBytes(1000.5, { locale: 'de-DE' }); // '1 kB'
| Feature | bytes | filesize | humanize-bytes | pretty-bytes |
|---|---|---|---|---|
| Parse String | ✅ Yes | ❌ No | ❌ No | ❌ No |
| Format Number | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
| Bits Support | ❌ No | ✅ Yes | ❌ No | ❌ No |
| Locale Support | ❌ No | ✅ Yes | ❌ No | ✅ Yes |
| Config Depth | Low | High | None | Medium |
| Primary Use | Backend/HTTP | Dashboards | Simple Scripts | Frontend UI |
These tools solve similar problems but fit different slots in your architecture.
bytes is the backend specialist 🖥️. Use it in Node.js services when you need to read configuration values like 512mb or parse HTTP headers. It is the only choice here that handles input parsing reliably.
filesize is the power user tool 🛠️. Reach for it when you need to display network speeds in bits, enforce specific rounding rules, or support obscure unit standards. It handles edge cases the others ignore.
humanize-bytes is the legacy lightweight 🪶. Only use it for quick internal scripts where dependencies must be minimal and features like localization do not matter. For new projects, prefer pretty-bytes.
pretty-bytes is the frontend standard 🎨. It is the default choice for React, Vue, or Angular apps. It provides localized, clean output without requiring complex configuration.
Final Thought: For most modern web apps, pair bytes on the server for parsing inputs and pretty-bytes on the client for displaying results. This combination covers the full lifecycle of data handling — from ingestion to presentation.
Choose bytes when working in Node.js environments, especially with Express, where you need to parse incoming byte strings from headers or configuration files. It is the only package in this group designed for bidirectional conversion (string to number and number to string). Use it for server-side logic where HTTP standards matter.
Choose filesize when you need granular control over output formatting, such as displaying network speeds in bits instead of bytes. It supports custom rounding, specific unit standards (JEDEC vs IEC), and locale formatting. It is ideal for dashboards or technical tools where precision and configuration are critical.
Choose humanize-bytes only for simple projects where you need a quick, zero-config solution and do not require locale support or advanced options. Be aware that it receives less maintenance than pretty-bytes, so it may not keep up with newer JavaScript standards. It works for internal tools where exact formatting rules are not strict.
Choose pretty-bytes for modern frontend applications where clean, localized UI text is the priority. It handles internationalization out of the box and follows current best practices for unit display. It is the default choice for build tools, download progress bars, and user-facing file size indicators.
Utility to parse a string bytes (ex: 1TB) to bytes (1099511627776) and vice-versa.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install command:
$ npm install bytes
var bytes = require('bytes');
Default export function. Delegates to either bytes.format or bytes.parse based on the type of value.
Arguments
| Name | Type | Description |
|---|---|---|
| value | number|string | Number value to format or string value to parse |
| options | Object | Conversion options for format |
Returns
| Name | Type | Description |
|---|---|---|
| results | string|number|null | Return null upon error. Numeric value in bytes, or string value otherwise. |
Example
bytes(1024);
// output: '1KB'
bytes('1KB');
// output: 1024
Format the given value in bytes into a string. If the value is negative, it is kept as such. If it is a float, it is rounded.
Arguments
| Name | Type | Description |
|---|---|---|
| value | number | Value in bytes |
| options | Object | Conversion options |
Options
| Property | Type | Description |
|---|---|---|
| decimalPlaces | number|null | Maximum number of decimal places to include in output. Default value to 2. |
| fixedDecimals | boolean|null | Whether to always display the maximum number of decimal places. Default value to false |
| thousandsSeparator | string|null | Example of values: ' ', ',' and '.'... Default value to ''. |
| unit | string|null | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to '' (which means auto detect). |
| unitSeparator | string|null | Separator to use between number and unit. Default value to ''. |
Returns
| Name | Type | Description |
|---|---|---|
| results | string|null | Return null upon error. String value otherwise. |
Example
bytes.format(1024);
// output: '1KB'
bytes.format(1000);
// output: '1000B'
bytes.format(1000, {thousandsSeparator: ' '});
// output: '1 000B'
bytes.format(1024 * 1.7, {decimalPlaces: 0});
// output: '2KB'
bytes.format(1024, {unitSeparator: ' '});
// output: '1 KB'
Parse the string value into an integer in bytes. If no unit is given, or value
is a number, it is assumed the value is in bytes.
Supported units and abbreviations are as follows and are case-insensitive:
b for byteskb for kilobytesmb for megabytesgb for gigabytestb for terabytespb for petabytesThe units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
Arguments
| Name | Type | Description |
|---|---|---|
| value | string|number | String to parse, or number in bytes. |
Returns
| Name | Type | Description |
|---|---|---|
| results | number|null | Return null upon error. Value in bytes otherwise. |
Example
bytes.parse('1KB');
// output: 1024
bytes.parse('1024');
// output: 1024
bytes.parse(1024);
// output: 1024