Node.js: Check if file is an symbolic link when iterating over directory with 'fs'
Asked Answered
F

2

24

Supervisor is a package for Node.js that monitors files in your app directory for modifications and reloads the app when a modification occurs.

This script interprets symbolic links as regular files and logs out a warning. I would like to fork Supervisor so that either this can be fixed entirely or that a more descriptive warning is produced.

How can I use the File System module of Node.js to determine if a given file is really an symbolic link?

Fogy answered 1/7, 2012 at 18:9 Comment(0)
B
41

You can use fs.lstat and then call statis.isSymbolicLink() on the fs.Stats object that's passed into your lstat callback.

fs.lstat('myfilename', function(err, stats) {
    console.log(stats.isSymbolicLink());
});
Baggett answered 2/7, 2012 at 1:38 Comment(0)
G
7

Seems like you can use isSymbolicLink()

const files = fs.readdirSync(dir, {encoding: 'utf8', withFileTypes: true});
files.forEach((file) => {
  if (file.isSymbolicLink()) {
    console.log('found symlink!');
  }
}
Guiltless answered 18/8, 2019 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.