Node.js check if path is file or directory

前端 未结 6 1365
小蘑菇
小蘑菇 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 10:00

    Depending on your needs, you can probably rely on node's path module.

    You may not be able to hit the filesystem (e.g. the file hasn't been created yet) and tbh you probably want to avoid hitting the filesystem unless you really need the extra validation. If you can make the assumption that what you are checking for follows . format, just look at the name.

    Obviously if you are looking for a file without an extname you will need to hit the filesystem to be sure. But keep it simple until you need more complicated.

    const path = require('path');
    
    function isFile(pathItem) {
      return !!path.extname(pathItem);
    }
    

提交回复
热议问题