The reason I ask, is because Node.js on ubuntu doesn't seem to have the fs.exists() function. Although I can call this when I run Node.js on my Mac, when I deploy to the server, it fails with an error saying the function does not exist.
Now, I am aware that some people consider it an "anti-pattern" 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 writing to them.
So how can I check if the directory (or file) exists ?
EDIT:
This is the code 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, it works fine. On ubuntu I get the 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
The fs. existsSync() method allows you to check for the existence of a file by tracing if a specified path can be accessed from the current directory where the script is executed. It returns true when the path exists and false when it's not. Create the assets/ folder if you want to see it logged to the console.
The simplest way to check if a certain directory exists in Node. js is by using the fs. existsSync() method. The existsSync() method returns true if the path exists, false otherwise.
So you can safely use fs. existsSync() to synchronously check if a file exists.
The fs. existsSync() method is used to synchronously check if a file already exists in the given path or not. It returns a boolean value which indicates the presence of a file.
It's 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
^^ That comment answers why it isn't there. I'll answer what you can do about it (besides not using ancient versions).
From the fs.exists()
documentation:
In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to
fs.exists()
andfs.open()
. Just open the file and handle the error when it's not there.
You could do something like this:
fs.open('mypath','r',function(err,fd){
if (err && err.code=='ENOENT') { /* file doesn't exist */ }
});
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 going 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 ES6 syntax + Promises - the sync version of this would be a bit simpler. Also my code also checks to see if there is a directory present in the path string and returns true if stat is happy with it - this may not be what everyone wants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With