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

白昼怎懂夜的黑 提交于 2020-01-03 08:34:13

问题


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?


回答1:


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());
});



回答2:


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!');
  }
}


来源:https://stackoverflow.com/questions/11284464/node-js-check-if-file-is-an-symbolic-link-when-iterating-over-directory-with-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!