node.js - node如何判断文件夹是否存在呢?
高洛峰
高洛峰 2017-04-17 15:30:36
0
4
709

console.log(fs.statSync('./_test/'));
如果_test不存在,会报错。并没有返回值,那么如何判断文件夹是否存在呢?


忘记说了我是node6, fs.exists这个文档已经明确指出 废弃了

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
巴扎黑

fs.exists is deprecated, it is recommended to use fs.stat and fs.access. The following is what I use in my project:

//检测文件或者文件夹存在 nodeJS
function fsExistsSync(path) {
    try{
        fs.accessSync(path,fs.F_OK);
    }catch(e){
        return false;
    }
    return true;
}

Of course, this is only the synchronous version. If you need an asynchronous version, you can modify it yourself~ If it is useful, please update it below your question so that future generations can view it.

洪涛

https://nodejs.org/dist/lates...
Here are detailed instructions, take a look

小葫芦

Use where node

刘奇

I remember that there is fs.exists and fs.existsSync, both of which can determine whether it exists. The latter one is synchronous

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!