Socket
Book a DemoInstallSign in
Socket

bytesizes

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bytesizes

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

2.1.5
latest
Source
npmnpm
Version published
Weekly downloads
4
100%
Maintainers
0
Weekly downloads
 
Created
Source

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!

Keywords

file-size

FAQs

Package last updated on 02 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.