1
const fs = require('fs')
const images="images"
const path ='images\palmtunnel.jpg'
if(fs.existsSync(path)){
    console.log('exist')
}
else{
    console.log('does not exist')
}

enter image description here

this way didn't work just working in the files in the same directory.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Beso Nour
  • 13
  • 4
  • Check this answer: https://stackoverflow.com/questions/17699599/node-js-check-if-file-exists – Vasilis Mar 03 '22 at 20:14
  • Does this answer your question? [Node.js check if file exists](https://stackoverflow.com/questions/17699599/node-js-check-if-file-exists) – Phil Mar 04 '22 at 03:17

1 Answers1

0

Have you tried using '/' intead of '\' ?
So your path will be:

const path = 'images/palmtunnel.jpg'

The backslash (\) is an escape character in Javascript (along with a lot of other C-like languages). This means that when Javascript encounters a backslash, it tries to escape the following character. For instance, \n is a newline character (rather than a backslash followed by the letter n). -Daniel JavaScript backslash (\) in variables is causing an error

BlueBeret
  • 474
  • 3
  • 23