![]() |
VOOZH | about |
The fs.statSync() method is used to synchronously return information about the given file path. The fs.Stat object returned has several fields and methods to get more details about the file.
fs.statSync( path, options )Parameters: This method accept two parameters as mentioned above and described below:
Returns: It returns a Stats object which contains the details of the file path.
Below examples illustrate the fs.statSync() method in Node.js:
Example 1: This example demonstrates how to use Node.js's fs.statSync() method to retrieve and display file and directory information synchronously.
Output:
Stats {
dev: 3229478529,
mode: 33206,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: 4096,
ino: 1970324837039946,
size: 0,
blocks: 0,
atimeMs: 1582306776282,
mtimeMs: 1582482953967,
ctimeMs: 1582482953968.2532,
birthtimeMs: 1582306776282.142,
atime: 2020-02-21T17:39:36.282Z,
mtime: 2020-02-23T18:35:53.967Z,
ctime: 2020-02-23T18:35:53.968Z,
birthtime: 2020-02-21T17:39:36.282Z
}
Path is file: true
Path is directory: false
Stats {
dev: 3229478529,
mode: 16822,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: 4096,
ino: 562949953486669,
size: 0,
blocks: 0,
atimeMs: 1582482965037.8445,
mtimeMs: 1581074249467.7114,
ctimeMs: 1582482964979.8303,
birthtimeMs: 1582306776288.1958,
atime: 2020-02-23T18:36:05.038Z,
mtime: 2020-02-07T11:17:29.468Z,
ctime: 2020-02-23T18:36:04.980Z,
birthtime: 2020-02-21T17:39:36.288Z
}
Path is file: false
Path is directory: true
Example 2: This example demonstrates how to use the Node.js fs.statSync() method to retrieve file statistics and the option to return values as big integers.
Output:
Stats {
dev: 3229478529,
mode: 33206,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: 4096,
ino: 1970324837039946,
size: 0,
blocks: 0,
atimeMs: 1582306776282,
mtimeMs: 1582482953967,
ctimeMs: 1582482953968.2532,
birthtimeMs: 1582306776282.142,
atime: 2020-02-21T17:39:36.282Z,
mtime: 2020-02-23T18:35:53.967Z,
ctime: 2020-02-23T18:35:53.968Z,
birthtime: 2020-02-21T17:39:36.282Z
}
BigIntStats {
dev: 3229478529n,
mode: 33206n,
nlink: 1n,
uid: 0n,
gid: 0n,
rdev: 0n,
blksize: 4096n,
ino: 1970324837039946n,
size: 0n,
blocks: 0n,
atimeMs: 1582306776282n,
mtimeMs: 1582482953967n,
ctimeMs: 1582482953968n,
birthtimeMs: 1582306776282n,
atimeNs: 1582306776282000000n,
mtimeNs: 1582482953967000000n,
ctimeNs: 1582482953968253100n,
birthtimeNs: 1582306776282142200n,
atime: 2020-02-21T17:39:36.282Z,
mtime: 2020-02-23T18:35:53.967Z,
ctime: 2020-02-23T18:35:53.968Z,
birthtime: 2020-02-21T17:39:36.282Z
}
The fs.statSync() method in Node.js provides detailed file and directory information synchronously. It's ideal for ensuring file stats are available before continuing but should be used cautiously in performance-sensitive situations.
Reference:https://nodejs.org/api/fs.html#fs_fs_statsync_path_options