humanbytes
Human-readable byte sizes. 1536 ⇄ '1.5 KB'.
import humanbytes from 'humanbytes'
humanbytes(1536) // '1.5 KB'
humanbytes('1.5 KB') // 1536
humanbytes('10mb') // 10485760
humanbytes('2 GiB') // 2147483648
humanbytes.format(1500, { standard: 'si' }) // '1.5 kB'
humanbytes.format(1536, { standard: 'iec' }) // '1.5 KiB'
humanbytes.format(1536, { long: true }) // '1.5 kilobytes'
- Both directions: parse strings, format numbers, with one function
- Every unit style:
KB (1024), kB (1000), KiB, bits (Mbit), long names (megabytes), Kubernetes/Docker (512Mi, 2g)
- You choose what
KB means: 1024 by default, like bytes; 1000 with standard: 'si'
- Rounds correctly: 1023.999 KB is
'1 MB', not '1024 KB'
- Huge sizes: up to yottabytes,
bigint in and out with no lost digits
- Typo-proof:
humanbytes('10 MBs') is a TypeScript error, and a runtime error that says Did you mean 'MB'?
- Any language:
{ locale: 'de' } → '1,5 KB'
- Zero dependencies, ~2.5 KB gzipped, ESM + CommonJS, Node 20+/Bun/Deno/browsers
Install
npm install humanbytes
Why not bytes / pretty-bytes?
|
bytes |
pretty-bytes |
humanbytes |
Format 1536 |
'1.5KB' |
'1.54 kB' |
'1.5 KB' (or '1.54 kB' with standard: 'si') |
Parse '1.5 KB' |
✅ |
❌ format only |
✅ |
1024-based (KB) / 1000-based (kB) / IEC (KiB) |
1024 only |
1000 or KiB |
all three |
Parse '2 GiB' |
❌ returns 2 |
❌ |
✅ 2147483648 |
Parse '10 parsecs' |
❌ returns 10 silently |
❌ |
throws a clear error (or use tryParse) |
| Format 1023.999 KB |
❌ '1024KB' |
✅ '1 MB' |
✅ '1 MB' |
| EB, ZB, YB |
❌ stops at PB |
✅ |
✅ |
bigint |
❌ returns null |
✅ format |
✅ format and parse, exact digits |
Bits (Mbit) |
❌ |
✅ |
✅ |
Long names ('1.5 kilobytes') |
❌ |
❌ |
✅ |
| Locale number formatting |
❌ |
✅ |
✅ |
CommonJS require() |
✅ |
❌ ESM only |
✅ |
| TypeScript types |
@types/bytes |
✅ |
✅ built in, literals type-checked |
Migrating from bytes: bytes(n) → humanbytes(n, { unitSeparator: '' }) and bytes.parse(s) → humanbytes(s). Differences: humanbytes adds a space by default, returns fractional bytes as-is (bytes floors them), and throws on invalid input instead of returning null.
Migrating from pretty-bytes: prettyBytes(n) → humanbytes(n, { standard: 'si' }) and prettyBytes(n, { binary: true }) → humanbytes(n, { standard: 'iec' }).
Which KB?
The same letters mean different sizes depending on who you ask, so humanbytes lets you pick with standard:
standard |
1 KB is |
Labels |
Used by |
'jedec' (default) |
1024 bytes |
KB, MB, GB |
Windows, RAM sizes, the bytes package |
'si' |
1000 bytes |
kB, MB, GB |
disk vendors, macOS, pretty-bytes |
'iec' |
1024 bytes |
KiB, MiB, GiB |
Linux tools, Kubernetes; never ambiguous |
The same option works for parsing: with 'si' or 'iec', an ambiguous KB or kilobyte is read as 1000 bytes (that is what it strictly means next to IEC units). IEC units like KiB are always 1024. So parse(format(n, opts), opts) always round-trips.
Parse
import { parse, tryParse, isByteSize, parseBigInt } from 'humanbytes'
parse('1.5 KB') // 1536
parse('1.5kb') // 1536 (case-insensitive, space optional)
parse('1.5 KB', { standard: 'si' }) // 1500
parse('2 GiB') // 2147483648 (IEC is always 1024-based)
parse('512 bytes') // 512
parse('1.5 kilobytes') // 1536
parse('100 Mbit', { standard: 'si' }) // 12500000 (bits)
parse('512Mi') // 536870912 (Kubernetes)
parse('2g') // 2147483648 (Docker, JVM)
parse('1,024 B') // 1024
parse('-1 KB') // -1024
parse('1024') // 1024 (plain numbers are bytes)
parse(1024) // 1024 (numbers pass through)
parse('1 bit') // 0.125 (not rounded; use Math.round for whole bytes)
parse('10 MBs') // throws BytesError: Unknown unit 'MBs' ... Did you mean 'MB'?
tryParse('nope') // undefined
isByteSize('10mb') // true
parseBigInt('16 EiB') // 18446744073709551616n (exact, rounded to whole bytes)
Units (any case): B, byte(s), bit(s), and for each prefix k, m, g, t, p, e, z, y: KB, KiB, K, Ki, Kbit, Kibit, kilobyte(s), kibibyte(s), kilobit(s), kibibit(s) and so on up to yotta/yobi.
Bits vs bytes
b and B both mean bytes by default, because '10mb' in a config file almost always means 10 megabytes. If you deal with network speeds where Mb means megabits, turn on caseSensitive:
parse('100 Mb') // 104857600 (bytes)
parse('100 Mb', { caseSensitive: true }) // 13107200 (megabits, 1024-based)
parse('100 MB', { caseSensitive: true }) // 104857600
bit / Mbit / megabits always mean bits.
Errors
Like durationly, invalid input throws a BytesError (a TypeError with an input property) instead of returning null or a wrong number. Use tryParse if you prefer undefined. NaN and Infinity throw in both parse and format.
TypeScript
String literals are checked at compile time. Values typed as plain string (env vars, config files) are accepted and checked at runtime:
humanbytes('10 MB') // ✅
humanbytes('10 MBs') // ❌ compile error
humanbytes(process.env.MAX_UPLOAD!) // ✅ checked at runtime
import type { ByteSize } from 'humanbytes'
interface Config { maxUpload: ByteSize } // number | '10 MB' | ...
Format
import { format } from 'humanbytes'
format(1536) // '1.5 KB'
format(1234567) // '1.18 MB'
format(1500, { standard: 'si' }) // '1.5 kB'
format(1536, { standard: 'iec' }) // '1.5 KiB'
format(1234567, { decimals: 0 }) // '1 MB'
format(1024, { fixedDecimals: true }) // '1.00 KB'
format(1536, { unitSeparator: '' }) // '1.5KB'
format(1536, { long: true }) // '1.5 kilobytes'
format(1073741824, { unit: 'MB' }) // '1024 MB'
format(125000, { bits: true, standard: 'si' }) // '1 Mbit'
format(1536, { signed: true }) // '+1.5 KB'
format(1536, { locale: 'de' }) // '1,5 KB'
format(1e9, { unit: 'KB', locale: 'en' }) // '976,562.5 KB'
format(2n ** 64n) // '16 EB'
format('1.5 GiB', { standard: 'iec' }) // '1.5 GiB' (strings are parsed first)
Values that round up to the next unit move to it: format(1048575) is '1 MB', not '1024 KB'. Sizes past yottabytes stay in YB: format(2 ** 100) is '1048576 YB'.
API
humanbytes(input, options?)
A string is parsed (same as parse) and returns a number. A number or bigint is formatted (same as format) and returns a string. The named functions below are also attached: humanbytes.parse, humanbytes.format, ...
parse(input, options?) → number
Parses a byte-size string into bytes. Numbers are returned unchanged. Throws BytesError on invalid input.
| Option |
Default |
|
standard |
'jedec' |
How to read KB, MB, kilobyte, k: 'jedec' = 1024, 'si' / 'iec' = 1000. KiB etc. are always 1024. |
caseSensitive |
false |
Read a lowercase b as bits: '100 Mb' is 100 megabits. |
The result is not rounded. Above Number.MAX_SAFE_INTEGER (8 PiB) numbers lose precision; use parseBigInt.
tryParse(input, options?) → number | undefined
Like parse, but returns undefined instead of throwing.
isByteSize(input, options?) → boolean
true if parse would accept the value.
parseBigInt(input, options?) → bigint
Like parse, but returns an exact whole number of bytes as a bigint (rounded half away from zero). Accepts strings, numbers and bigints.
format(input, options?) → string
Formats a number of bytes, a bigint, or a byte-size string. Throws BytesError for NaN, Infinity and non-numbers.
| Option |
Default |
|
standard |
'jedec' |
'jedec' (1024, KB), 'si' (1000, kB) or 'iec' (1024, KiB). |
decimals |
2 |
Maximum digits after the decimal point (0-20). |
fixedDecimals |
false |
Keep trailing zeros: '1.50 KB'. |
unitSeparator |
' ' |
Text between number and unit. |
unit |
best fit |
Always use this unit, e.g. 'MB', 'GiB', 'kbit'. IEC units are always 1024-based; bit units turn on bits. |
bits |
false |
Show bits: 1000 → '7.81 Kbit'. The input is still bytes. |
signed |
false |
Add + to positive values. Zero never gets a sign. |
locale |
none |
Format the number with Intl.NumberFormat: a locale string or array, or true for the runtime default. Unit labels are not translated. |
long |
false |
Long unit names: '1 kilobyte', '1.5 kibibytes', '8 bits'. |
BytesError
Thrown for invalid input. Extends TypeError; the bad value is in error.input.
Types
Standard, Unit, ByteSize, ByteString, FormatOptions, ParseOptions, CheckByteSize<T>, IsValidByteSize<T>.
Real-world examples
app.use(express.json({ limit: humanbytes(process.env.BODY_LIMIT ?? '1 MB') }))
if (file.size > humanbytes('25 MB')) throw new Error(`Max upload is ${humanbytes(humanbytes('25 MB'))}`)
console.log(`Downloaded ${format(received)} of ${format(total)}`)
console.log(`Bundle grew by ${format(after - before, { signed: true, standard: 'si' })}`)
CommonJS
const humanbytes = require('humanbytes')
humanbytes(1536) // '1.5 KB'
humanbytes.parse('1 GB')
License
MIT