Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Last active July 17, 2023 10:19
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save FGRibreau/3323836 to your computer and use it in GitHub Desktop.
Save FGRibreau/3323836 to your computer and use it in GitHub Desktop.
existsSync - Check if a file exist in NodeJS
/*
fileExistSync - Check if a file exist in NodeJS
Twitter: @FGRibreau / fgribreau.com
Usage:
var fileExistSync = require('./fileExistSync');
var exist = fileExistSync('/var/folders/zm/jmjb49l172g6g/T/65b199');
Support for Nodev0.6
*/
var fs = require('fs');
module.exports = fs.existsSync || function existsSync(filePath){
try{
fs.statSync(filePath);
}catch(err){
if(err.code == 'ENOENT') return false;
}
return true;
};
@vjpr
Copy link

vjpr commented Nov 1, 2017

Should add throw err if code is not ENOENT otherwise you are potentially silencing errors and returning the wrong result.

@BhargavMantha
Copy link

i agree with vjpr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment