Check synchronously if file/directory exists in Node.js

前端 未结 15 2136
梦如初夏
梦如初夏 2020-11-22 12:26

How can I synchronously check, using node.js, if a file or directory exists?

15条回答
  •  死守一世寂寞
    2020-11-22 13:13

    Chances are, if you want to know if a file exists, you plan to require it if it does.

    function getFile(path){
        try{
            return require(path);
        }catch(e){
            return false;
        }
    }
    

提交回复
热议问题