Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 607 Bytes

section4.6.md

File metadata and controls

31 lines (26 loc) · 607 Bytes

Section 4.6: Checking if a file or a directory exists

Asynchronously

const fs = require('fs');

fs.stat('path/to/file', function(err) {
  if (!err) {
    console.log('file or directory exists');
  }
  else if (err.code === 'ENOENT') {
    console.log('file or directory does not exist');
  }
});

Synchronously

here, we must wrap the function call in a try/catch block to handle error.

try {
  fs.statSync('path/to/file');
  console.log('file or directory exists');
}
catch (err) {
  if (err.code === 'ENOENT') {
    console.log('file or directory does not exist');
  }
}