Open In App

Node.js fs.lstatSync() Method

The fs.lstatSync() method is used to synchronously return information about the symbolic link that is being used to refer to a file or directory. The fs.Stat object returns several fields and methods to get more details about the file.

Syntax:



fs.lstatSync( path, options )

Parameters: This method accept two parameters as mentioned above and described below:

Returns: It returns a fs.Stats object which contains the details of the symbolic link.



Below examples illustrate the fs.lstatSync() method in Node.js:

Example 1: This example uses fs.lstatSync() method to get the details of a symbolic link to a file.




// Node.js program to demonstrate the
// fs.lstatSync() method
  
// Import the filesystem module
const fs = require('fs');
  
fs.symlinkSync(__dirname + "\\example_file.txt",
                       "symlinkToFile", 'file');
  
console.log("Symlink to file created")
  
statsObj = fs.lstatSync("symlinkToFile");
  
console.log("Stat of symlinkToFile")
console.log(statsObj);

Output:

Symlink to file created
Stat of symlinkToFile
Stats {
  dev: 3229478529,
  mode: 41398,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: 4096,
  ino: 281474976780954,
  size: 49,
  blocks: 0,
  atimeMs: 1585207963423.2476,
  mtimeMs: 1585207963423.2476,
  ctimeMs: 1585207963423.2476,
  birthtimeMs: 1585207963423.2476,
  atime: 2020-03-26T07:32:43.423Z,
  mtime: 2020-03-26T07:32:43.423Z,
  ctime: 2020-03-26T07:32:43.423Z,
  birthtime: 2020-03-26T07:32:43.423Z
}

Example 2: This example uses fs.lstatSync() method to get the details of a symbolic link to a folder.




// Node.js program to demonstrate the
// fs.lstatSync() method
  
// Import the filesystem module
const fs = require('fs');
  
fs.symlinkSync(__dirname + "\\example_directory",
                          "symlinkToDir", 'dir');
  
console.log("Symlink to directory created")
  
statsObj = fs.lstatSync("symlinkToDir");
console.log("Stat of symlinkToDir")
console.log(statsObj);

Output:

Stat of symlinkToDir
Stats {
  dev: 3229478529,
  mode: 41398,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: 4096,
  ino: 281474976780955,
  size: 50,
  blocks: 0,
  atimeMs: 1585208001112.3284,
  mtimeMs: 1585208001112.3284,
  ctimeMs: 1585208001112.3284,
  birthtimeMs: 1585208001112.3284,
  atime: 2020-03-26T07:33:21.112Z,
  mtime: 2020-03-26T07:33:21.112Z,
  ctime: 2020-03-26T07:33:21.112Z,
  birthtime: 2020-03-26T07:33:21.112Z
}

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


Article Tags :