The `bytes` package (current stable version 3.1.2) is a focused JavaScript utility designed for converting human-readable byte strings (e.g., "1KB", "5.5GB") into their numerical byte equivalents, and vice-versa. It provides a simple, unified API that delegates to a `format` function for converting numbers to strings and a `parse` function for converting strings or numbers to bytes. A key differentiator is its strict adherence to powers of two for unit conversions (e.g., 1KB equals 1024 bytes), which is critical for accurate storage and memory calculations, distinguishing it from decimal-based systems. While major releases are not frequent, the library is stable and widely used, typically receiving updates for bug fixes or minor enhancements. It supports various units including B, KB, MB, GB, TB, and PB, and offers formatting options such as decimal places, thousands separators, and custom unit separators.
npm install bytesVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing byte strings to numbers, formatting numbers to byte strings, and using specific `format` and `parse` functions with various options and error handling.
Always be mindful of the base (1024) when using this library. Do not use it if a base-10 (1000) conversion is required.
Always check the return value for `null` after calling `bytes()`, `bytes.parse()`, or `bytes.format()` to ensure a valid conversion occurred: `const value = bytes('invalid'); if (value === null) { /* handle error */ }`Remove `unit: 'b'` from your `bytes.format` options. The function will automatically format values in bytes if they are less than 1KB. For values requiring a specific unit, use 'KB', 'MB', etc., but 'B' will be inferred.
Ensure you are importing the default export correctly: `import bytes from 'bytes';` (ESM) or `const bytes = require('bytes');` (CommonJS).For ESM projects, use `import bytes from 'bytes';`. If you must use `require` in an ESM file, consider converting the file to CommonJS or using a build tool that handles CJS-ESM interoperability.
Verify that `import bytes from 'bytes';` or `const bytes = require('bytes');` correctly assigns the module to the `bytes` variable before attempting to call its properties like `format` or `parse`.No dependency data recorded yet.