DEV Community

AKSH DESAI
AKSH DESAI

Posted on

Checking if a file or a directory exists in node js

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

Thank You.
You can follow us on:
Youtube
Instagram

Top comments (0)