The stats.mode property is an inbuilt application programming interface of the fs.Stats class which is used to get the file type and mode as bit-field.
Syntax:
javascript
Output:
javascript
Output:
stats.mode;Return Value: It returns a number or BigInt value that represents a bit-field that describes the file type and mode. Below examples illustrate the use of stats.mode in Node.js: Example 1:
// Node.js program to demonstrate the
// stats.mode property
// Accessing fs module
const fs = require('fs');
// Calling fs.Stats stats.mode for files
// using stat
fs.stat('./filename.txt', (err, stats) => {
if (err) throw err;
console.log("using stat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
if (err) throw err;
console.log("using lstat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
// For directories
// Using stat
fs.stat('./', (err, stats) => {
if (err) throw err;
console.log("using stat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
// Using lstat
fs.lstat('./', (err, stats) => {
if (err) throw err;
console.log("using lstat: the type and "
+ "mode bit-field of the file is "
+ stats.mode);
});
using stat: the type and mode bit-field of the file is 33206 using lstat: the type and mode bit-field of the file is 33206 using stat: the type and mode bit-field of the file is 16822 using lstat: the type and mode bit-field of the file is 16822Example 2:
// Node.js program to demonstrate the
// stats.mode property
// Accessing fs module
const fs = require('fs').promises;
// Calling fs.Stats stats.mode
(async() => {
const stats = await fs.stat('./filename.txt');
console.log("using stat synchronous: the type "
+ "and mode bit-field of the file is "
+ stats.mode);
})().catch(console.error)
(node:9448) ExperimentalWarning: The fs.promises API is experimental using stat synchronous: the type and mode bit-field of the file is 33206Note: The above program will compile and run by using the
node filename.js command and use the file_path correctly.
Reference: https://nodejs.org/api/fs.html#fs_stats_mode