Node.js file stats
Every file comes with a set of details that we can inspect using Node.js. In particular, using the stat()
method provided by the fs
module.
You call it passing a file path, and once Node.js gets the file details it will call the callback function you pass, with 2 parameters: an error message, and the file stats:
const import fs
fs = require: any
require('node:fs');
import fs
fs.stat('/Users/joe/test.txt', (err: any
err, stats: any
stats) => {
if (err: any
err) {
var console: Console
console.Console.error(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(err: any
err);
}
// we have access to the file stats in `stats`
});
Node.js also provides a sync method, which blocks the thread until the file stats are ready:
const import fs
fs = require: any
require('node:fs');
try {
const const stats: any
stats = import fs
fs.statSync('/Users/joe/test.txt');
} catch (var err: unknown
err) {
var console: Console
console.Console.error(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(var err: unknown
err);
}
The file information is included in the stats variable. What kind of information can we extract using the stats?
A lot, including:
- if the file is a directory or a file, using
stats.isFile()
andstats.isDirectory()
- if the file is a symbolic link using
stats.isSymbolicLink()
- the file size in bytes using
stats.size
.
There are other advanced methods, but the bulk of what you'll use in your day-to-day programming is this.
const import fs
fs = require: any
require('node:fs');
import fs
fs.stat('/Users/joe/test.txt', (err: any
err, stats: any
stats) => {
if (err: any
err) {
var console: Console
console.Console.error(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(err: any
err);
return;
}
stats: any
stats.isFile(); // true
stats: any
stats.isDirectory(); // false
stats: any
stats.isSymbolicLink(); // false
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(stats: any
stats.size); // 1024000 //= 1MB
});
You can also use promise-based fsPromises.stat()
method offered by the fs/promises
module if you like:
const import fs
fs = require: any
require('node:fs/promises');
async function function example(): Promise<void>
example() {
try {
const const stats: any
stats = await import fs
fs.stat('/Users/joe/test.txt');
const stats: any
stats.isFile(); // true
const stats: any
stats.isDirectory(); // false
const stats: any
stats.isSymbolicLink(); // false
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(const stats: any
stats.size); // 1024000 //= 1MB
} catch (function (local var) err: unknown
err) {
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(function (local var) err: unknown
err);
}
}
function example(): Promise<void>
example();
You can read more about the fs
module in the official documentation.