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:

  • path: It is a String, Buffer or URL that holds the path of the symbolic link.
  • options: It is an object that can be used to specify optional parameters that will affect the output. It has one optional parameter:
    • bigint: It is a boolean value which specifies if the numeric values returned in the fs.Stats object are bigint. The default value is false.

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.