Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

The stats.uid property is an inbuilt application programming interface of the fs.Stats class is used to get the numeric (number / bigint) identity of the user to which the file belongs to.

Syntax:

stats.uid;

Return Value: It returns a number or BigInt value which represents the identity of the user that owns the file.

Below examples illustrate the use of stats.uid property in Node.js:

Example 1:




// Node.js program to demonstrate the   
// stats.uid property
  
// Accessing fs module
const fs = require('fs');
  
// Calling fs.Stats stats.uid
//for directory using stat
fs.stat('./', (err, stats) => {
  if (err) throw err;
  console.log("using stat: numeric "
    + "identity of the user is "
    + stats.uid);
});
   
//using lstat
fs.lstat('./', (err, stats) => {
  if (err) throw err;
  console.log("using lstat: numeric "
    + "identity of the user is "
    + stats.uid);
});
   
// For file
// Using stat
fs.stat('./filename.txt', (err, stats) => {
  if (err) throw err;
  console.log("using stat: numeric "
    + "identity of the user is "
    + stats.uid);
});
   
// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
  if (err) throw err;
  console.log("using lstat: numeric identity"
    + " of the user is  "+stats.uid);
});


Output:

using stat: numeric identity of the user is  9932440
using lstat: numeric identity of the user is  9932440
using stat: numeric identity of the user is  9932440
using lstat: numeric identity of the user is  9932440

Example 2:




// Node.js program to demonstrate the   
// stats.uid property
  
// Accessing fs module
const fs = require('fs').promises;
   
// Calling fs.Stats stats.uid
(async() => {
    const stats = await fs.stat('./filename.txt');
  console.log("using stat synchronous: numeric "
    + "identity of the user is  "+stats.uid);
})().catch(console.error)


Output:

(node:14456) ExperimentalWarning: The fs.promises API 
is experimental 
using stat synchronous: numeric identity of the user is 9932440

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_uid



Last Updated : 26 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads