https://github.com/dawsbot/file-bytes
Get a file's size with Node.js
https://github.com/dawsbot/file-bytes
bytes filebytes filesize filesystem fs nodejs
Last synced: 9 months ago
JSON representation
Get a file's size with Node.js
- Host: GitHub
- URL: https://github.com/dawsbot/file-bytes
- Owner: dawsbot
- License: mit
- Created: 2016-05-10T03:56:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-03T06:33:48.000Z (about 3 years ago)
- Last Synced: 2024-11-01T06:36:05.128Z (about 1 year ago)
- Topics: bytes, filebytes, filesize, filesystem, fs, nodejs
- Language: JavaScript
- Homepage:
- Size: 380 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# file-bytes
[](https://www.npmjs.com/package/file-bytes)
[](https://ci.appveyor.com/project/dawsonbotsford/file-bytes)
[](https://github.com/sindresorhus/xo)
> In JavaScript - Get the bytes size of a file
## Install
```
npm install --save file-bytes
```
## Usage
```js
const fileBytes = require('file-bytes');
fileBytes('README.md').then(size => {
console.log(`README.md is ${size} bytes`);
});
//=> "README.md" is 1806 bytes
// sync
console.log(`README.md is ${fileBytes.sync('README.md')} bytes`);
//=> "README.md" is 1806 bytes
```
## API
### fileBytes(filename)
Returns a promise for the filename's file size as a `number`.
### fileBytes.sync(filename)
Return's the filename's file size as a `number`.
#### filename
Type: `string`
## Examples
* Convert the output into human style using [pretty-bytes](https://github.com/sindresorhus/pretty-bytes)
```js
const fileBytes = require('file-bytes');
const prettyBytes = require('pretty-bytes');
console.log(prettyBytes(fileBytes.sync('example.txt')));
//=> 1.34 kB
```
* Get the file sizes of everything in the current repo with [glob](https://github.com/isaacs/node-glob)
```js
const fileBytes = require('file-bytes');
const glob = require('glob');
glob('./*', (err, files) => {
// files is an array of filenames.
for (let file of files) {
const size = fileBytes.sync(file);
console.log(`"${file}" is ${size} bytes`);
}
});
//=> "README.md" is 1806 bytes
//=> "package.json" is 122 bytes
//=> "index.js" is 497 bytes
```
## License
MIT © [Dawson Botsford](https://dawsbot.com)