Node.js check if file exists
Consider opening the file directly, to avoid race conditions: const fs = require(‘fs’); fs.open(‘foo.txt’, ‘r’, function (err, fd) { // … }); Using fs.existsSync: if (fs.existsSync(‘foo.txt’)) { // … } Using fs.stat: fs.stat(‘foo.txt’, function(err, stat) { if (err == null) { console.log(‘File exists’); } else if (err.code === ‘ENOENT’) { // file does not exist … Read more