EN
Node.js - file stats
0 points
In this article, we would like to show you how to get file stats in Node.js.
1. Import fs
module in your project
xxxxxxxxxx
1
const fs = require('fs');
2. Use fs.stat()
to get the file stats.
In the example below, we print in the console example stats for .txt
file with a callback function specified as fs.stat()
method second argument.
xxxxxxxxxx
1
const fs = require('fs');
2
3
fs.stat('./file.txt', (err, stats) => {
4
if (err) {
5
console.error(err);
6
return;
7
}
8
9
console.log('isFile: ' + stats.isFile()); //true
10
console.log('isDirectory: ' + stats.isDirectory()); //false
11
console.log('isSymbolicLink: ' + stats.isSymbolicLink()); //false
12
console.log('size: ' + stats.size); // size in bytes
13
});
Example output for file.txt:
xxxxxxxxxx
1
isFile: true
2
isDirectory: false
3
isSymbolicLink: false
4
size: 4