# diskusage

> Get total diskspace and free diskspace using bindings around platform specific calls.

Latest version **1.2.0** (published 2023-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install diskusage
pnpm add diskusage
yarn add diskusage
bun add diskusage
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2023-09-15 |
| First published | 2014-01-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 157 |
| Author | jduncanator |
| Maintainers | jduncanator |
| Keywords | disk, usage, df, hdd, ssd, diskusage, df, free, space |

## Links

- npm: https://www.npmjs.com/package/diskusage
- Repository: https://github.com/jduncanator/node-diskusage
- Issues: https://github.com/jduncanator/node-diskusage/issues
- npm.io page: https://npm.io/package/diskusage

## Dependencies (2)

- [nan](https://npm.io/package/nan.md) ^2.18.0
- [es6-promise](https://npm.io/package/es6-promise.md) ^4.2.8

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2023-09-15
- 1.1.3 — 2019-07-08
- 1.1.2 — 2019-06-17
- 1.1.1 — 2019-04-30
- 1.1.0 — 2019-04-18
- 1.0.0 — 2018-12-03
- 0.2.6 — 2018-10-31
- 0.2.5 — 2018-09-25
- 0.2.4 — 2017-10-05
- 0.2.3 — 2017-08-28
- 0.2.2 — 2017-05-03
- 0.2.1 — 2017-02-01
- 0.2.0 — 2017-01-22
- 0.1.5 — 2016-02-26
- 0.1.4 — 2015-11-09
- … 4 more at https://npm.io/package/diskusage/versions

## README

node-diskusage
==============

<p align="center">
  <a href="https://www.npmjs.com/package/diskusage"><img src="https://img.shields.io/npm/v/diskusage.svg" alt="npm Version" title="npm Version" /></a>
  <a href="https://npm-stat.com/charts.html?package=diskusage"><img src="https://img.shields.io/npm/dw/diskusage.svg" alt="npm Downloads" title="npm Downloads" /></a>
</p>

This module implements platform specific bindings to obtain disk usage information on Windows and POSIX platforms. Windows support is backed by [GetDiskFreeSpaceEx](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937/) and POSIX is implemented with [statvfs](http://www.freebsd.org/cgi/man.cgi?query=statvfs).

Installation
------------

Install with `npm`:

``` bash
$ npm install diskusage
```

Usage
--------

The module exposes two functions. `check` takes a path/mount point as the first argument and a callback as the second. The callback takes two arguments `err` and `info`. `err` will be an `Error` if something went wrong. `info` contains three members: `available`, `free` and `total` in bytes.

If no callback is supplied `check` will instead return a `Promise<DiskUsage>` that you can await.

- `available`: Disk space available to the current user (i.e. Linux reserves 5% for root)
- `free`: Disk space physically free
- `total`: Total disk space (free + used)

`checkSync` only takes the path argument. It returns the same `info` on success, throws an `Error` on failure.

Examples
--------

``` js
const disk = require('diskusage');
const os = require('os');

let path = os.platform() === 'win32' ? 'c:' : '/';

// Callbacks
disk.check(path, function(err, info) {
  if (err) {
    console.log(err);
  } else {
    console.log(info.available);
    console.log(info.free);
    console.log(info.total);
  }
});

// Promise
async function getFreeSpace(path) {
  try {
    const { free } = await disk.check(path);
    console.log(`Free space: ${free}`);
    return free
  } catch (err) {
    console.error(err)
    return 0
  }
}

// Or without using async/await
disk.check(path)
  .then(info => console.log(`free: ${info.free}`))
  .catch(err => console.error(err))

// Synchronous
try {
  let info = disk.checkSync(path);
  console.log(info.available);
  console.log(info.free);
  console.log(info.total);
}
catch (err) {
  console.log(err);
}
```

TypeScript
----------

The module has an embedded .d.ts file. You can use `import * as diskusage from 'diskusage'`.

```ts
type DiskUsage = {
    available: number;
    free: number;
    total: number;
}

export function check(path: string, callback: (error?: Error, result?: DiskUsage) => void): void;
export function check(path: string): Promise<DiskUsage>
export function checkSync(path: string): DiskUsage;
```

Demo
----

To see a demo of this library see the `demo/` folder.

You can run it with node: (node 8+ required)

```bash
node ./demo/
```

---
_Source: https://npm.io/package/diskusage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
