Checking if a file or directory is in node js. exists or not

Javascript code:-

const fs = require('fs')

// check if directory exists
const dir = './uploads'
if (fs.existsSync(dir)) {
  console.log('Directory exists!')
} else {
  console.log('Directory not found.')
}

// ---------------------------------------------------------------

// check if File exists
const path = "./package.json"
if (fs.existsSync(path)) {
    console.log('File exists!')
  } else {
    console.log('File not found.')
  }
enter fullscreen mode

exit fullscreen mode

Thanks.
You can follow us on:
youtube
instagram

Leave a Comment