How to check if a file or directory exists without using fs.exists?

I ask because Node.js on ubuntu does not seem to have the fs.exists () function. Although I can call it when I run Node.js on my Mac, when I deploy the server, it fails with the message that the function does not exist.

Now I know that some people consider an “anti-template” to check if a file exists, and then try and edit / open it, etc., but in my case I never delete these files, but I still need to check if they exist before they are written.

So how can I check if a directory (or file) exists?

EDIT:

This is the code that I run in a file called "temp.'s":

var fs=require('fs'); fs.exists('./temp.js',function(exists){ if(exists){ console.log('yes'); }else{ console.log("no"); } }); 

On my Mac, this works fine. On ubuntu, I get an error:

 node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: Object #<Object> has no method 'exists' at Object.<anonymous> (/home/banana/temp.js:2:4) at Module._compile (module.js:441:26) at Object..js (module.js:459:10) at Module.load (module.js:348:32) at Function._load (module.js:308:12) at Array.0 (module.js:479:10) at EventEmitter._tickCallback (node.js:192:41) 

On my Mac version: v0.13.0-pre On Ubuntu version: v0.6.12

+5
source share
3 answers

Probably due to the fact that in NodeJs 0.6 the exists() method was located in the path module: http://web.archive.org/web/20111230180637/http://nodejs.org/api/path.html - try -catch-finally

^^ This comment answers why it is not there. I will answer what you can do about it (besides using ancient versions).

From the fs.exists() documentation :

In particular, checking for a file before opening it is an anti-template that leaves you vulnerable to race conditions: another process may delete the file between fs.exists() and fs.open() calls. Just open the file and handle the error if it is not there.

You can do something like this:

 fs.open('mypath','r',function(err,fd){ if (err && err.code=='ENOENT') { /* file doesn't exist */ } }); 
+7
source

The accepted answer does not take into account that the node fs module documentation recommends using fs.stat to replace fs.exists (see the documentation ).

I ended up with this:

 function filePathExists(filePath) { return new Promise((resolve, reject) => { fs.stat(filePath, (err, stats) => { if (err && err.code === 'ENOENT') { return resolve(false); } else if (err) { return reject(err); } if (stats.isFile() || stats.isDirectory()) { return resolve(true); } }); }); } 

Note Syntax ES6 + Promises - The synchronization version of this will be a little easier. Also my code also checks if the directory exists in the path string and returns true if stat is satisfied with this - it may not be what everyone wants.

+6
source

Synchronization methods have no way to report an error. Exceptions! As it turned out, the fs.statSync method throws an exception if the file or directory does not exist. Creating a synchronization version is just as easy:

  function checkDirectorySync(directory) { try { fs.statSync(directory); } catch(e) { try { fs.mkdirSync(directory); } catch(e) { return e; } } } 

What is it. Usage is just as simple:

 checkDirectorySync("./logs"); //directory created / exists, all good. 

[] 'z

0
source

Source: https://habr.com/ru/post/1208996/


All Articles