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 | nullerr, stats: fs.Statsstats) => { if (err: NodeJS.ErrnoException | nullerr) { var console: Consoleconsole.Console.error(message?: any, ...optionalParams: any[]): void (+1 overload)error(err: NodeJS.ErrnoExceptionerr); } // 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.Statsstats = 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: unknownerr) { var console: Consoleconsole.Console.error(message?: any, ...optionalParams: any[]): void (+1 overload)error(var err: unknownerr); }

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() and stats.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 | nullerr, stats: fs.Statsstats) => { if (err: NodeJS.ErrnoException | nullerr) { var console: Consoleconsole.Console.error(message?: any, ...optionalParams: any[]): void (+1 overload)error(err: NodeJS.ErrnoExceptionerr); return; } stats: fs.Statsstats.StatsBase<number>.isFile(): booleanisFile(); // true stats: fs.Statsstats.StatsBase<number>.isDirectory(): booleanisDirectory(); // false stats: fs.Statsstats.StatsBase<number>.isSymbolicLink(): booleanisSymbolicLink(); // false var console: Consoleconsole.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)log(stats: fs.Statsstats.StatsBase<number>.size: numbersize); // 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: Statsstats = 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: Statsstats.StatsBase<number>.isFile(): booleanisFile(); // true const stats: Statsstats.StatsBase<number>.isDirectory(): booleanisDirectory(); // false const stats: Statsstats.StatsBase<number>.isSymbolicLink(): booleanisSymbolicLink(); // false var console: Consoleconsole.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)log(const stats: Statsstats.StatsBase<number>.size: numbersize); // 1024000 //= 1MB } catch (function (local var) err: unknownerr) { var console: Consoleconsole.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)log(function (local var) err: unknownerr); } } function example(): Promise<void>example();

You can read more about the fs module in the official documentation.

阅读时间
2分钟
作者
贡献
编辑此页