Node.js check if path is file or directory

前端 未结 6 1366
小蘑菇
小蘑菇 2020-12-12 09:23

I can\'t seem to get any search results that explain how to do this.

All I want to do is be able to know if a given path is a file or a directory (folder).

6条回答
  •  既然无缘
    2020-12-12 09:35

    Seriously, question exists five years and no nice facade?

    function is_dir(path) {
        try {
            var stat = fs.lstatSync(path);
            return stat.isDirectory();
        } catch (e) {
            // lstatSync throws an error if path doesn't exist
            return false;
        }
    }
    

提交回复
热议问题