Open In App

Node.js stats.mode Property from fs.Stats Class

Last Updated : 25 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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:
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: javascript
// 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);
});
Output:
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  16822
Example 2: javascript
// 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)
Output:
(node:9448) ExperimentalWarning: The fs.promises API
is experimental 
using stat synchronous: the type and mode bit-field 
of the file is  33206
Note: 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

Next Article

Similar Reads