Open In App

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

Last Updated : 26 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The stats.gid property is an inbuilt application programming interface of the fs.Stats class which is used to get the numeric (number / bigint) identity of the group to which the file belongs to. Syntax:
stats.gid;
Return Value: It returns a number or BigInt value which represents the identity of the group that owns the file. Below examples illustrate the use of stats.gid property in Node.js: Example 1: javascript
// Node.js program to demonstrate the   
// stats.gid property

// Accessing fs module
const fs = require('fs');

// Calling fs.Stats stats.gid
// for directory using stat
fs.stat('./', (err, stats) => {
    if (err) throw err;
    console.log("using stat: numeric "
        + "identity of the group is "
        + stats.gid);
});

// Using lstat
fs.lstat('./', (err, stats) => {
    if (err) throw err;
    console.log("using lstat: numeric "
        + "identity of the group is "
        + stats.gid);
});

// For file
// Using stat
fs.stat('./filename.txt', (err, stats) => {
    if (err) throw err;
    console.log("using stat: numeric "
        + "identity of the group is "
        + stats.gid);
});

// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
    if (err) throw err;
    console.log("using lstat: numeric "
        + "identity of the group is "
        + stats.gid);
});
Output:
using stat: numeric identity of the group is  5687
using lstat: numeric identity of the group is  5687
using stat: numeric identity of the group is  5687
using lstat: numeric identity of the group is  5687
Example 2: javascript
// Node.js program to demonstrate the   
// stats.gid property

// Accessing fs module
const fs = require('fs').promises;

// Calling fs.Stats stats.gid
(async () => {
    const stats = await fs.stat('./filename.txt');
    console.log("using stat synchronous: numeric"
        + " identity of the group is  " + stats.gid);
})().catch(console.error)
Output:
(node:15204) ExperimentalWarning: The fs.promises API 
is experimental 
using stat synchronous: numeric identity of the group is 5687
Note: The above program will compile and run by using the node filename.js command and use the file_path correctly. This API will work correctly for POSIX system. In other systems like WINDOWS it will return 0. Reference: https://nodejs.org/api/fs.html#fs_stats_gid

Similar Reads