Open In App

Node.js fsPromises.lstat() Method

Last Updated : 18 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.promises.lstat() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computers. The lstat() method gives some information specific to files and folders using methods defines on stats objects (data provided by lstat). The fs.promises.lstat() method returns a resolved or rejected promise and hence avoids the callback nesting or callback hell problems that may occur in fs.readdir() method.
Syntax 
 

fs.promises.lstat(path, options)

Parameter: This method accepts two parameters as mentioned above and described below: 
path: It is a string, buffer or url that specifies the path to the directory, whose contents we try to read. 
options: It is an optional parameter. One options parameter is ‘bigint’, it is a boolean value. Here, we specify if the numeric value in returned stats object by fs.lstat() method then it is bigint or not(default-false).
Return Value: It returns a resolved or rejected promise. The promise is resolved with stats object if the directory is successfully read otherwise rejected with an error object if any error is occurred (example-specified directory not exist or does not have permissions to read files, etc). 
stats object returned from the resolved promise has some properties and methods defined in it which helps in getting some specific details about the target files or folders. Some of the methods are specified below.
 

  • stats.isDirectory(): Returns true if stats object describes a file system directory.
  • stats.isFile(): Returns true if stats object describes a regular file.
  • stats.isSocket(): Returns true if stats object describes a socket.
  • stats.isSymbolicLink(): Returns true if stats object describes a symbolic link.
  • stats.isFile(): Returns true if stats object describes a regular file.
  • stats.isFIFO(): Returns true if stats object describes first in first out pipe.
  • stats.size: Specifies the size of the file in bytes.
  • stats.blocks: Specifies the number of blocks allocated for the file.

Example 1:
 

javascript




// Node.js program to demonstrate the   
// Buffer.from() Method 
  
// Importing File System module
const fs = require('fs')
  
// fs.readdir() reads contents of 
// target directory 
// process.cwd() gives current 
// working directory
fs.readdir(process.cwd(), (err, filenames) => {
  if (err) {
    console.log(err)
    return
  }
  
  for (let filename of filenames) {
  
    // Calling lstat method to give the 
    // stats object for every directory
    fs.promises.lstat(filename)
  
      // If promise resolved and data
      // are fetched
      .then(stats => {
        if (stats.isFile()) {
          console.log(`${filename} ---------> File`)
        } else {
          console.log(`${filename} ---------> Folder`)
        }
      })
  
      // If promise is rejected
      .catch(err => {
        console.log(err)
      })
  }
})


Output: 
 

Example 2: 
 

javascript




// Node.js program to demonstrate the   
// Buffer.from() Method 
  
// Importing File System module
const fs = require('fs')
  
// The fs.readdir() method reads the 
// contents of target directory
// The process.cwd() method gives the
// current working directory
fs.readdir(process.cwd(), async (err, filenames) => {
  if (err) {
    console.log(err)
    return
  }
  for (let filename of filenames) {
  
    // Calling lstat method to give the
    // stats object for every directory
    fs.promises.lstat(filename)
  
      // If promise resolved and datas
      // are fetched
      .then(stats => {
        console.log(
          `${filename} --------> ${stats.size} bytes`)
      })
  
      // If promise is rejected
      .catch(err => {
        console.log(err)
      })
  }
})


Output: 
 

Reference: https://nodejs.org/api/fs.html#fs_fspromises_lstat_path_options
 



Previous Article
Next Article

Similar Reads

Node.js fs.lstat() Method
The fs.lstat() method is similar to the fs.stat() method except that it is used to return information about the symbolic link that is being used to refer to a file or directory. The fs.Stat object returned has several fields and methods to get more details about the file. Syntax: fs.lstat( path, options, callback ) Parameters: This method accept th
2 min read
How to operate callback-based fs.lstat() method with promises in Node.js ?
The fs.lstat() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The lstat() method gives some information specific to files and folders using methods define on stats objects (data provided by lstat).The fs.lstat() method is based on callback. Using cal
4 min read
PHP | lstat( ) function
The lstat() function in PHP is used to return information about a file or a symbolic link. It gathers statistics of the file which is sent as a parameter to the lstat() function. The function returns an array which includes information on following elements : [0] or [dev] - Device number[1] or [ino] - Inode number[2] or [mode] - Inode protection mo
4 min read
Node.js fsPromises.truncate() Method
The fsPromises.truncate() method in node.js is used to change the size of the file i.e. either increase or decrease the file size. This method changes the length of the file at the path by len bytes. If len represents a length shorter than the file’s current length, the file is truncated to that length. If it is greater than the file length is padd
2 min read
Node.js fsPromises.chown() Method
The fsPromises.chown() method is used to change the ownership of a file then resolves the Promise with no arguments upon success. The function accepts a user id and group id that can be used to set the respective owner and group. Syntax: fsPromises.chown( path, uid, gid) Parameters: This method accepts threeparameters as mentioned above and describ
2 min read
Node.js fsPromises.mkdir() Method
The fsPromises.mkdir() method is used to asynchronously create a directory then resolves the Promise with either no arguments, or the first directory path created if recursive is true. Syntax: fsPromises.mkdir(path, options) Parameters: This method accept two parameters as mentioned above and described below: path: This parameter is a String, Buffe
1 min read
Node.js fsPromises.mkdtemp() Method
The fsPromises.mkdtemp() method is an inbuilt method which creates a unique temporary directory and resolves the Promise with the created directory path. Syntax: fs.Promises.mkdtemp( prefix, options ) Parameters: The method accepts two parameters as mentioned above and described below: prefix: It is a string that denotes the path of the file. optio
2 min read
Node.js fsPromises.realpath() Method
The fsPromises.realPath() method determines the actual location of path using the same semantics as the fs.realpath.native() function then resolves the Promise with the resolved path. Only paths that can be converted to UTF8 strings are supported. Syntax: fsPromises.realpath( path, options ) Parameters: This method accepts two parameters as mention
2 min read
Node.js fsPromises.access() Method
The fsPromises.access() method is used to test the permissions of a given file or directory specified by path. The permissions to be checked can be specified as a parameter using file access constants. It is also possible to check multiple file permissions by using the bitwise OR operator to create a mask with more than one file constant. If the ac
3 min read
Node.js fsPromises.open() Method
The fsPromises.open() method is used to asynchronously open a file that returns a Promise that, when resolved, yields a FileHandle object. Syntax: fsPromises.open( filename, flags, mode) Parameter: This method accepts three parameters as mentioned above and described below: filename: It is a String, Buffer, or URL that holds the name of the file to
2 min read