Skip to content
master
Switch branches/tags
Go to file
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
 
 
 
 
 
 
 
 
 
 
 
 

get-folder-size

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

⚠️ Version 3 of this package completely redesigns the API to use ES6 promises and bumps minimum node support to version 14. If you need to use the old callback syntax, or if you need to support an older version of node, 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

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

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', 
  {
    ignore: /pattern/,
    fs: customFS,
  }
)

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 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