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.’)
}

// ————————————–…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by AKSH DESAI

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.')
  }

Thank You.
You can follow us on:
Youtube
Instagram


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by AKSH DESAI


Print Share Comment Cite Upload Translate Updates
APA

AKSH DESAI | Sciencx (2022-11-12T11:50:36+00:00) Checking if a file or a directory exists in node js. Retrieved from https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/

MLA
" » Checking if a file or a directory exists in node js." AKSH DESAI | Sciencx - Saturday November 12, 2022, https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/
HARVARD
AKSH DESAI | Sciencx Saturday November 12, 2022 » Checking if a file or a directory exists in node js., viewed ,<https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/>
VANCOUVER
AKSH DESAI | Sciencx - » Checking if a file or a directory exists in node js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/
CHICAGO
" » Checking if a file or a directory exists in node js." AKSH DESAI | Sciencx - Accessed . https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/
IEEE
" » Checking if a file or a directory exists in node js." AKSH DESAI | Sciencx [Online]. Available: https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/. [Accessed: ]
rf:citation
» Checking if a file or a directory exists in node js | AKSH DESAI | Sciencx | https://www.scien.cx/2022/11/12/checking-if-a-file-or-a-directory-exists-in-node-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.