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 module "node:fs"
fs = var require: NodeJS.Require
(id: string) => any
require('node:fs');
module "node:fs"
fs.function stat(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, stats: fs.Stats) => void): void (+3 overloads)
stat('/Users/joe/test.txt', (err: NodeJS.ErrnoException | null
err, stats: fs.Stats
stats) => {
if (err: NodeJS.ErrnoException | null
err) {
var console: Console
console.Console.error(message?: any, ...optionalParams: any[]): void (+1 overload)
error(err: NodeJS.ErrnoException
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 module "node:fs"
fs = var require: NodeJS.Require
(id: string) => any
require('node:fs');
try {
const const stats: fs.Stats
stats = module "node:fs"
fs.const statSync: fs.StatSyncFn
(path: fs.PathLike, options?: undefined) => fs.Stats (+6 overloads)
statSync('/Users/joe/test.txt');
} catch (var err: unknown
err) {
var console: Console
console.Console.error(message?: any, ...optionalParams: any[]): void (+1 overload)
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 module "node:fs"
fs = var require: NodeJS.Require
(id: string) => any
require('node:fs');
module "node:fs"
fs.function stat(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, stats: fs.Stats) => void): void (+3 overloads)
stat('/Users/joe/test.txt', (err: NodeJS.ErrnoException | null
err, stats: fs.Stats
stats) => {
if (err: NodeJS.ErrnoException | null
err) {
var console: Console
console.Console.error(message?: any, ...optionalParams: any[]): void (+1 overload)
error(err: NodeJS.ErrnoException
err);
return;
}
stats: fs.Stats
stats.StatsBase<number>.isFile(): boolean
isFile(); // true
stats: fs.Stats
stats.StatsBase<number>.isDirectory(): boolean
isDirectory(); // false
stats: fs.Stats
stats.StatsBase<number>.isSymbolicLink(): boolean
isSymbolicLink(); // false
var console: Console
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
log(stats: fs.Stats
stats.StatsBase<number>.size: number
size); // 1024000 //= 1MB
});
You can also use promise-based fsPromises.stat()
method offered by the fs/promises
module if you like:
const module "node:fs/promises"
fs = var require: NodeJS.Require
(id: string) => any
require('node:fs/promises');
async function function example(): Promise<void>
example() {
try {
const const stats: Stats
stats = await module "node:fs/promises"
fs.function stat(path: PathLike, opts?: StatOptions & {
bigint?: false | undefined;
}): Promise<Stats> (+2 overloads)
stat('/Users/joe/test.txt');
const stats: Stats
stats.StatsBase<number>.isFile(): boolean
isFile(); // true
const stats: Stats
stats.StatsBase<number>.isDirectory(): boolean
isDirectory(); // false
const stats: Stats
stats.StatsBase<number>.isSymbolicLink(): boolean
isSymbolicLink(); // false
var console: Console
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
log(const stats: Stats
stats.StatsBase<number>.size: number
size); // 1024000 //= 1MB
} catch (function (local var) err: unknown
err) {
var console: Console
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
log(function (local var) err: unknown
err);
}
}
function example(): Promise<void>
example();
You can read more about the fs
module in the official documentation.