Node.js check if file exists

前端 未结 17 897
生来不讨喜
生来不讨喜 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:38

    Well I did it this way, as seen on https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback

    fs.access('./settings', fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK, function(err){
      console.log(err ? 'no access or dir doesnt exist' : 'R/W ok');
    
      if(err && err.code === 'ENOENT'){
        fs.mkdir('settings');
      }
    });
    

    Is there any problem with this?

提交回复
热议问题