bytes
Tiny utility for parsing and formatting byte strings like '1kb'
Repository Health
Technical Analysis
bytes is a single-purpose Node.js utility that converts between numeric byte counts and human-readable byte strings — bytes.format(1024) returns '1KB', and bytes.parse('1kb') returns 1024. It supports the standard binary-prefix units (b, kb, mb, gb, tb, pb, using 1024-based multipliers) plus formatting options for decimal places, thousands separators, and unit separators.
At under 200 lines with zero runtime dependencies, it’s the kind of small, dependable utility that gets pulled in transitively by dozens of larger frameworks (including Express-ecosystem middleware) anywhere a byte size needs to move between a config string and a number.
What You Get
bytes.format(value, options)- convert a byte count into a formatted string like'1.5MB'bytes.parse(value)- convert a byte string like'1kb'back into a numeric byte count- Format options for decimal places, fixed decimals, thousands separator, and unit separator
- Support for all standard binary-prefix units from bytes through petabytes
- Zero runtime dependencies and a small, auditable single-file implementation
Common Use Cases
- Parsing human-friendly config values like
maxFileSize: '10mb'into a numeric byte limit for upload middleware - Displaying human-readable file or transfer sizes in CLI tools and server logs
- Validating request body size limits in HTTP frameworks (used transitively by several Express/Connect body-parsing middlewares)
- Converting between raw byte counts and display strings in dashboards or admin UIs
Under The Hood
Architecture - The entire implementation lives in a single index.js: a fixed map object defines 1024-based multipliers per unit, format() picks the largest unit that fits the magnitude (or uses an explicit options.unit) and formats via toFixed plus optional thousands-separator regex post-processing, while parse() runs a single regex (parseRegExp) to extract a numeric value and unit suffix, falling back to a plain parseInt if the string doesn’t match a unit pattern.
Tech Stack - Plain, dependency-free JavaScript (CommonJS), with a test/ suite and standard .eslintrc linting; no build step is needed since the published package is the source file itself.
Code Quality - The code is compact, well-commented with JSDoc-style annotations for both public functions, and has a long-lived History.md documenting every release since 2012, suggesting careful, incremental maintenance rather than churn; there’s no runtime dependency to audit, which minimizes supply-chain surface area for consumers.
API Design - The API is about as low-friction as a utility can be: one function call in, one value out, with the same default export accepting either a string or number and dispatching accordingly, which is likely why it became a common transitive dependency across body-parsing and HTTP middleware in the Node ecosystem.
Used by 11 apps in this directory
twenty
CRM
The open-source CRM you build, ship, and version like the rest of your stack — with customizable objects, AI agents, and a TypeScript SDK.
Webiny JS
Blogging · CMS · Ecommerce
Open-source, self-hosted CMS on AWS serverless — a TypeScript framework you extend with code, not a product you configure through a UI.