Filesize Utility
The `filesize` library is a lightweight, high-performance JavaScript utility designed to convert byte values into human-readable strings, such as "1.02 kB" or "1 MiB". Currently at version 11.0.15, it emphasizes reliability and efficiency with zero external dependencies and 100% test coverage. Key differentiators include full TypeScript support, robust internationalization via the `Intl` API, `BigInt` compatibility for extremely large numbers, and support for multiple unit standards (SI, IEC, JEDEC). It offers a functional API with partial application for creating reusable formatters and is fully compatible with both Node.js and browser environments. While a specific release cadence isn't published, the project appears actively maintained with regular updates.
Common errors
-
TypeError: filesize is not a function
cause Incorrect CommonJS `require` or mixed ESM/CJS environment trying to access a default export that no longer exists.fixFor ESM, use `import { filesize } from 'filesize';`. For CommonJS (since v10), use `const { filesize } = require('filesize');`. -
TypeError: Cannot read properties of undefined (reading 'filesize')
cause Attempting to destructure a CommonJS `require` statement where the package was imported as a default, or the default export was expected.fixEnsure that `filesize` is explicitly imported as a named export. In CommonJS, this means `const { filesize } = require('filesize');`. -
TypeError: filesize called on non-object
cause Passing an invalid or non-numeric value (e.g., `null`, `undefined`, a string that cannot be parsed) to the `filesize` function.fixEnsure the input to `filesize` is a valid `number` or `bigint`.
Warnings
- breaking Since v10.0.0, the CommonJS export is no longer a default export and requires named destructuring, e.g., `const { filesize } = require('filesize');`. Older versions allowed `const filesize = require('filesize');`.
- breaking Version 10.0.0 dropped support for Node.js 12 and 14. Users on these Node.js versions should remain on `filesize` < v10 or upgrade their Node.js environment.
- breaking Version 9.0.0 changed the default standard to SI (base 10) from previous defaults, and removed the 'suffix' option. It also dropped support for Node.js 10 and 12.
- gotcha When using `standard: "iec"`, it is crucial to also set `base: 2` to ensure correct binary (1024-based) calculations and symbols (KiB, MiB). Otherwise, `standard: "iec"` with `base: -1` (auto, often 10) might produce unexpected results or use SI (decimal) calculations with IEC symbols.
Install
-
npm install filesize -
yarn add filesize -
pnpm add filesize
Imports
- filesize
import filesize from 'filesize';
import { filesize } from 'filesize'; - partial
const partial = require('filesize');import { partial } from 'filesize'; - FileSizeOptions
import type { FileSizeOptions } from 'filesize';
Quickstart
import { filesize, partial } from 'filesize';
// Convert bytes to a human-readable string using default (SI) standard
const sizeInBytes = 265318;
console.log(`Default SI format: ${filesize(sizeInBytes)}`); // "265.32 kB"
// Use IEC standard with base 2 for binary sizes
const binarySize = 1048576; // 1 MiB
console.log(`IEC binary format: ${filesize(binarySize, { standard: 'iec', base: 2 })}`); // "1 MiB"
// Create a partially applied formatter for consistent styling
const formatBinaryIEC = partial({ standard: 'iec', base: 2, round: 0, spacer: '' });
console.log(`Formatted with partial application: ${formatBinaryIEC(binarySize)}`); // "1MiB"
// Output as an object for programmatic access
const sizeObject = filesize(1536, { output: 'object' });
console.log(`Output as object: ${JSON.stringify(sizeObject)}`); // {"value":1.54,"symbol":"kB","exponent":1,"unit":"kB"}