Node.js check if file exists

前端 未结 17 929
生来不讨喜
生来不讨喜 2020-12-07 10:02

How do i check the existence of a file?

In the documentation for the module fs there\'s a description of the method fs.exists(path, cal

17条回答
  •  生来不讨喜
    2020-12-07 10:41

      fs.statSync(path, function(err, stat){
          if(err == null) {
              console.log('File exists');
              //code when all ok
          }else if (err.code == "ENOENT") {
            //file doesn't exist
            console.log('not file');
    
          }
          else {
            console.log('Some other error: ', err.code);
          }
        });
    

提交回复
热议问题