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 🙏

© 2024 – Pkg Stats / Ryan Hefner

bytesizes

v2.1.5

Published

A utility to check the size of files or folders and convert sizes between different units.

Downloads

15

Readme

ByteSizes

NPM Version Repository Size License Downloads Support

ByteSizes is a lightweight and powerful Node.js package for checking the size of files or folders with the ability to easily convert between different units. It's designed to help developers manage and monitor filesystem sizes with a simple, intuitive interface and efficient error handling.

Table of Contents

Features

  • File Size Detection: Quickly get the size of individual files.
  • Folder Size Calculation: Compute the total size of folders, including all subdirectories.
  • Multiple Unit Conversions: Convert between bytes, KB, MB, GB, TB, PB, and EB.
  • Robust Error Handling: Get clear error messages and stack traces for easy debugging.

Installation

Install ByteSizes using npm:

npm install bytesizes

Usage

Here's a simple example demonstrating how to use ByteSizes:

const ByteSizes = require('bytesizes');

(async () => {
  try {
    // Get file size and convert it to KB
    const fileSize = await ByteSizes.getFileSize('example.txt');
    console.log(`File Size: ${ByteSizes.convertSize(fileSize, 'KB')}`);
    
    // Get folder size and convert it to MB
    const folderSize = await ByteSizes.getFolderSize('myFolder');
    console.log(`Folder Size: ${ByteSizes.convertSize(folderSize, 'MB')}`);
  } catch (error) {
    console.error('Error:', error.message);
  }
})();

TypeScript & ES Modules Usage

If you're using TypeScript or ES Modules (.mjs), you can import the module as follows:

import ByteSizes from 'bytesizes';

// Same usage as above

API Reference

getFileSize(filePath: string): Promise<number>

  • Description: Returns the size of a file in bytes.
  • Parameters:
    • filePath: The path to the file.
  • Returns: A promise resolving to the file size in bytes.

getFolderSize(folderPath: string): Promise<number>

  • Description: Calculates the total size of a folder (including subfolders) in bytes.
  • Parameters:
    • folderPath: The path to the folder.
  • Returns: A promise resolving to the folder size in bytes.

convertSize(sizeInBytes: number, unit: string): string

  • Description: Converts the size from bytes to the specified unit.
  • Parameters:
    • sizeInBytes: The size in bytes to convert.
    • unit: The unit to convert to (bytes, KB, MB, GB, TB, PB, EB).
  • Returns: A formatted string showing the converted size.

Error Handling

ByteSizes ensures clear and descriptive error messages for easier troubleshooting. When an error occurs, the stack trace is provided for better context.

Example:

✖ ERROR: Error reading file or directory: example.txt
  └─ Stack Trace:
Error: ENOENT: no such file or directory, open 'D:\Path\To\example.txt'

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! If you'd like to contribute, please open an issue or submit a pull request on GitHub. All contributions must follow coding best practices and include relevant test cases.

Acknowledgments

  • Node.js - A JavaScript runtime for building scalable network applications.
  • fs.promises - The Promise-based Node.js filesystem module.
  • Special thanks to everyone who has contributed to this package!