Registry / serialization / filesize

filesize

JSON →
library11.0.15jsnpmunverified

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.

npm install filesize
INSTALL
IMPORT
SIG · FILESIZE
F
filesize
serializationjavascriptv11.0.15
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

filesize
import { filesize } from 'filesize';
import filesize from 'filesize';
Since v10.0.0, both ESM and CommonJS imports require named destructuring. The `import filesize from 'filesize'` syntax is incorrect for ESM, and `const filesize = require('filesize');` is incorrect for CJS.
partial
import { partial } from 'filesize';
const partial = require('filesize');
Similar to `filesize`, `partial` is a named export. Ensure named destructuring for both ESM and CommonJS after v10.0.0.
FileSizeOptions
import type { FileSizeOptions } from 'filesize';
Import types separately using `import type` for explicit type annotations in TypeScript environments.

Demonstrates basic filesize conversion, using different standards (SI, IEC), partial application for reusable formatters, and various output formats including string and object.

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"}
Debug
Known issues
breakingSince 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');`.
fix
Adjust CommonJS `require` statements to use named destructuring: `const { filesize, partial } = require('filesize');`
affects: >=10.0.0
breakingVersion 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.
fix
Upgrade Node.js to version 16 or higher, or pin the `filesize` package to a version less than 10.0.0.
affects: >=10.0.0
breakingVersion 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.
fix
Explicitly set the `standard` option if non-SI behavior is desired (e.g., `{ standard: 'iec', base: 2 }`). If relying on the 'suffix' option, refactor your code as it is no longer available.
affects: >=9.0.0
gotchaWhen 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.
fix
Always specify `{ standard: 'iec', base: 2 }` together for accurate binary IEC units.
affects: All versions
Errors
Common errors & fixes
TypeError: filesize is not a function
Incorrect CommonJS `require` or mixed ESM/CJS environment trying to access a default export that no longer exists.
fix
For ESM, use `import { filesize } from 'filesize';`. For CommonJS (since v10), use `const { filesize } = require('filesize');`.
TypeError: Cannot read properties of undefined (reading 'filesize')
Attempting to destructure a CommonJS `require` statement where the package was imported as a default, or the default export was expected.
fix
Ensure that `filesize` is explicitly imported as a named export. In CommonJS, this means `const { filesize } = require('filesize');`.
TypeError: filesize called on non-object
Passing an invalid or non-numeric value (e.g., `null`, `undefined`, a string that cannot be parsed) to the `filesize` function.
fix
Ensure the input to `filesize` is a valid `number` or `bigint`.
Upgrade
Version history
11.0.15latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
filesize — npm install filesize · libregistry