fs - Node.js check exist file -
how check existence of file?
in documentation module fs
there's description of method fs.exists(path, callback)
. but, understand, checks existence of directories. , need check file!
how can done?
why not try opening file ? fs.open('yourfile', 'a', function (err, fd) { ... })
anyway after minute search try :
var path = require('path'); path.exists('foo.txt', function(exists) { if (exists) { // } }); // or if (path.existssync('foo.txt')) { // }
for node.js v0.12.x , higher
both path.exists
, fs.exists
have been deprecated
using fs.stat:
fs.stat('foo.txt', function(err, stat) { if(err == null) { console.log('file exists'); } else if(err.code == 'enoent') { // file not exist fs.writefile('log.txt', 'some log\n'); } else { console.log('some other error: ', err.code); } });
Comments
Post a Comment