1

i use this code to specify whether or not a file exists:

if(fs.exists('../../upload/images/book/2.jpg')){
    console.log("yes the file exist");
} else{
        console.log("there is no file");
      }  

While the file is there, it always says it does not exist(there is no file) i use fs.stat(...) too , but again it gives the result (there is no file)

Folder structure:

root_app  
--------|.tmp  
--------|api  
------------|controllers  
------------------------|BookContoller.js (My code run from here)
--------|...(And the rest of the folders)  
--------|upload  
---------------|images  
----------------------|book  
---------------------------|2.jpg

thank you

mas cm
  • 131
  • 2
  • 10
  • Where are you calling this code from, can you show us by way of editing your question to include the app structure and the location of the file with the code relative to the upload folder? – chridam Feb 11 '18 at 16:33
  • I'd suggest looking at this `console.log(path.resolve('../../upload/images/book/2.jpg'))` and see exactly what absolute path that is. Note: you will have to do `const path = require('path')` first to load the `path` module. – jfriend00 Feb 11 '18 at 16:36
  • Also depending on which version of node you are using, [`fs.exists()`](https://nodejs.org/api/fs.html#fs_fs_existssync_path) is deprecated – chridam Feb 11 '18 at 16:37
  • @ chridam i edit my question and my node version is 9.4.0 and sails.js is v 0.12.14 – mas cm Feb 11 '18 at 16:46
  • 1
    The `..` stuff is going to be relative to the current directory which will depend upon how you start your node.js app. If you want something to be relative to the module directory, then you should use `__dirname` as the base part of the path. – jfriend00 Feb 11 '18 at 16:47
  • @ jfriend00 path.resolve get me: C:\Users\My user name\Desktop\upload\images\book\2.jpg – mas cm Feb 11 '18 at 17:07
  • Possible duplicate of [Check synchronously if file/directory exists in Node.js](https://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js) – paulogdm Feb 12 '18 at 00:53

2 Answers2

1

fs.exists is asynchronous. To make your code work you could just change it to fs.existsSync.

Also you have the wrong path to your file, you need to use path.resolve or __dirname, so the script will know where to find the file.

if(fs.existsSync(path.resolve('../../upload/images/book/2.jpg'))) {

See documentation:

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0

My problem was solved.
Apparently, we should use fs.accessSync() OR fs.access() instead of fs.exists()
Check out this link

This procedure can also be performed:

function Custom_existsSync(filename) {
  try {
    fs.accessSync(filename);
    return true;
  } catch(ex) {
    return false;
  }  

if(Custom_existsSync(filename)){
    ....
}  

Thanks to everyone who helped me

mas cm
  • 131
  • 2
  • 10
  • 1
    `fs.exists` does not return a value. You can only get the result when it is passed to the callback `fs.exists(path, (result) => {});` – BrunoLM Feb 11 '18 at 20:16