npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tkh44/bytes

v1.0.0

Published

A tiny and fast utility to convert bytes to and from strings.

Readme

bytes

A tiny and fast utility to convert bytes to and from strings.


NOTICE: This is adapted from @lukeed/ms! I loved the API and simplicity of ms for time conversions, but needed something similar for file sizes and byte conversions. This library follows the same patterns but works with bytes (B, KB, MB, GB, TB, PB) instead of time units.


This module is delivered as:

Install

$ npm install --save @tkh44/bytes

Usage

import { parse, format, bytes } from '@tkh44/bytes';

// string => number
parse('1kb');       //=> 1024
parse('1mb');       //=> 1048576
parse('10gb');      //=> 10737418240
parse('2.5 mb');    //=> 2621440
parse('1tb');       //=> 1099511627776
parse('5b');        //=> 5
parse('1pb');       //=> 1125899906842624
parse('100');       //=> 100
parse('-1kb');      //=> -1024
parse('-200');      //=> -200

// tagged template literal
bytes`10MB`;        //=> 10485760
bytes`1kb`;         //=> 1024
bytes`${10}GB`;     //=> 10737418240
bytes`2.5 mb`;      //=> 2621440

// number => string
format(1024);              //=> '1KB'
format(2 * 1024);          //=> '2KB'
format(-3 * 1024);         //=> '-3KB'
format(parse('10 gb'));    //=> '10GB'

// number => string (long)
format(1024, true);              //=> '1 kilobyte'
format(2 * 1024, true);          //=> '2 kilobytes'
format(-3 * 1024, true);         //=> '-3 kilobytes'
format(parse('10 gb'), true);    //=> '10 gigabytes'

API

bytes.parse(input)

Returns: Number| undefined

Parses the input string, returning the number of bytes or undefined if the value can't be parsed successfully.

input

Type: String

The human-readable size string; eg: 10mb, 10MB, 10 megabytes.

bytes.format(bytes, long?)

Returns: String

Formats the byte count to a human-readable size string.

Important: The output will be rounded to the nearest whole integer.

bytes

Type: Number

The number of bytes.

long

Type: Boolean Default: false

Whether or not the output should use the unit's long/full form; eg kilobyte or kilobytes instead of KB.

Note: When long, the count and unit will be separated by a single space.Also, when long, the unit may be pluralized; eg 1 kilobyte vs 2 kilobytes.

bytes`input`

Returns: Number| undefined

Tagged template literal that parses size strings. Accepts both static strings and template interpolations.

bytes`10MB`        // => 10485760
bytes`${10}GB`     // => 10737418240
bytes`1.5 kb`      // => 1536

This is syntactic sugar for parse() and provides a cleaner API for static size values.

Use Cases

Never write this again:

// Before
maxBuffer: 1024 * 1024 * 10  // 10MB

// After (using parse)
import { parse } from '@tkh44/bytes';
maxBuffer: parse('10MB')

// After (using tagged template - even cleaner!)
import { bytes } from '@tkh44/bytes';
maxBuffer: bytes`10MB`

Credits

This library is adapted from @lukeed/ms by Luke Edwards. The structure, API design, and implementation approach are directly inspired by that excellent library, but adapted for byte/file size conversions instead of time conversions.

License

MIT © Kye Hohenberger