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

get-folder-size

v4.0.0

Published

Get the size of a folder by iterating through its sub-files and -folders.

Downloads

310,501

Readme

get-folder-size

Get the size of a folder by iterating through its sub-files and -folders.

| :warning: | Version 3+ of this package is pure ESM and uses a new promise-based API. If you need to use CommonJS or the old callback syntax, keep your dependency pinned to version ^2 (v2 API reference). | |-----------|----------------------------------------------------------------------------------------|

Basic usage

If you don't care about the details and just want a quick implementation, you can use:

getFolderSize.loose('/path/to/folder');

Example:

import getFolderSize from 'get-folder-size';

const myFolder = '/path/to/my/folder';

const size = await getFolderSize.loose(myFolder);
console.log(`The folder is ${size} bytes large`);
console.log(`That is the same as ${(size / 1000 / 1000).toFixed(2)} MB`);

Methods

When reading the size of a folder, read errors can randomly occur for a number of reasons, especially if a different process is altering files in the same folder at the same time. There are three different ways to call this package, depending on how you want to handle those errors:

getFolderSize(path, [options]): object

The default method will return an object with the size of the folder and a list of encountered errors:

{
  size: 1435,
  errors: [
    Error{} ...
  ]
}

If no errors were encountered, errors will be null. If errors were encountered, size will likely be smaller than the real folder size.

This method is great if you want to implement custom logic based on the errors that were encountered.

getFolderSize.loose(path, [options]): number | bigint

The loose method will return the folder size directly and ignore any errors it encounters, which means the returned folder size could be smaller than the real folder size.

This method is great if the precise size isn't too important, for example when used only to display the folder size to the user.

getFolderSize.strict(path, [options]): number | bigint

The strict method will return the folder size directly, but throw an error if it encounters any read errors.

This method is great if you need a very accurate number. You will have to implement some sort of error handling to use it reliably.

Options

Any of the three methods can also take an options object:

getFolderSize(
  '/path/to/folder', 
  {
    bigint: true,
    ignore: /pattern/,
    fs: customFS,
  }
)

If the bigint option is set to true, the folder size is returned as a BigInt instead of the default Number.

The ignore option takes a regex pattern. Any file or folder with a path that matches the pattern will not be counted in the total folder size.

The fs option allows you to pass a different filesystem handler, such as memfs, that will be used to read the folder size. The filesystem handler must incorporate lstat and readdir promise functions.

CLI tool

You can run this module from your command line:

get-folder-size --folder="/my/folder" --ignore="node_modules"

The optional ignore statement takes a regex pattern.

FAQ

I don't care if I have a file or folder, I just want to get the size.

If a file is passed to get-folder-size, it will simply return the size of the file. This means you can use it as a catch-all to get the size of any element in the filesystem.

Example:

import getItemSize from 'get-folder-size';

for(const path of [
  '/path/to/small/file.txt',
  '/path/to/small/folder/',
  '/path/to/large/file.js',
  '/path/to/large/folder/',
]){
  console.log(await getItemSize.strict(path));
}

// Console:
// 273
// 402
// 348614
// 674362319

Does it return actual size or size on disk?

This module calculates the actual folder size, and not the size on disk. Read about the difference here.

How do I import it from a CommonJS module?

CommonJS modules do not support the import..from method, but they do support this method:

const getFolderSize = (await import("get-folder-size")).default;

Note that this import only works inside an async function.

If you want to use the require method, consider just staying on v2. You can make v2 return a promise by importing it this way:

const util = require("util");
const getFolderSize = util.promisify(require("get-folder-size"));

If none of these methods work for you, send us a detailed explanation of your issue, and we will take a look at it.

How do I use it?

This is a Node module. If you are not sure what that means, please check out one of the many great tutorials online, like nodejs.dev.

When you have Node set up, you can install get-folder-size from your command line with this command:

npm install get-folder-size

You can now import it into your JavaScript files, or you can use its command line interface (CLI).

License

MIT